numpy - Trying to find the second largest value after grouping by even number python -


import numpy np  import pandas pd  values = np.array([1, 3, 2, 4, 1, 6, 4]) example_df = pd.dataframe({     'value': values,     'even': values % 2 == 0,     'above_three': values > 3  }, index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])  # find second largest value in each group if true:     def second_largest(xs):         sorted_xs = xs.sort(inplace=false, ascending=false)         return sorted_xs.iloc[1]  grouped_data = example_df.groupby('even') print grouped_data['value'].apply(second_largest) 

the traceback says following

attributeerror                       traceback (most recent call last) <ipython-input-94-251c7e3ea488> in 

() 14 15 grouped_data = example_df.groupby('even') ---> 16 print grouped_data['value'].apply(second_largest)

c:\programdata\anaconda2\lib\site-packages\pandas\core\groupby.pyc in 

apply(self, func, *args, **kwargs) 714 # ignore settingwithcopy here in case user mutates 715 option_context('mode.chained_assignment', none): --> 716 return self._python_apply_general(f) 717 718 def _python_apply_general(self, f):

c:\programdata\anaconda2\lib\site-packages\pandas\core\groupby.pyc in 

_python_apply_general(self, f) 718 def _python_apply_general(self, f): 719 keys, values, mutated = self.grouper.apply(f, self._selected_obj, --> 720 self.axis) 721 722 return self._wrap_applied_output(

c:\programdata\anaconda2\lib\site-packages\pandas\core\groupby.pyc in 

apply(self, f, data, axis) 1800 # group might modified 1801 group_axes = _get_axes(group) -> 1802 res = f(group) 1803 if not _is_indexed_like(res, group_axes): 1804 mutated = true

in second_largest(xs) 9 if true: 10 def second_largest(xs): ---> 11 sorted_xs = xs.sort(inplace=false, ascending=false) 12 print sorted_xs 13 return sorted_xs.iloc[1]

c:\programdata\anaconda2\lib\site-packages\pandas\core\generic.pyc in 

getattr(self, name) 3079 if name in self._info_axis: 3080 return self[name] -> 3081 return object.getattribute(self, name) 3082 3083 def setattr(self, name, value):

attributeerror: 'series' object has no attribute 'sort' 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -