Q:

TypeError: () got an unexpected keyword argument 'axis' when using an apply followed by a groupby

Am trying to extract values from a pandas Dataframe which are split by an ID. However when I feed the apply groupby, it wont let me provide an axis argument to apply the function row wise 
raw_data = {"id":{"0":"mergedshape_route_0009","1":"mergedshape_route_0009","2":"mergedshape_route_0009","3":"mergedshape_route_0009","4":"mergedshape_route_0009","5":"mergedshape_route_0009","6":"mergedshape_route_0009","7":"mergedshape_route_0009","8":"mergedshape_route_0009","9":"mergedshape_route_0009"},"shape_pt_lat":{"0":-6.8196991355,"1":-6.8194035167,"2":-6.8192916609,"3":-6.8192597021,"4":-6.8193262829,"5":-6.819395527,"6":-6.8194451,"7":-6.8192582,"8":-6.8184049,"9":-6.8177623},"shape_pt_lon":{"0":39.2987716198,"1":39.2989432812,"2":39.299055934,"3":39.29918468,"4":39.2993053794,"5":39.2993938923,"6":39.2994472,"7":39.2995691,"8":39.2999065,"9":39.2986298},"shape_pt_sequence":{"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9},"shape_dist_traveled":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0}}

df = pd.DataFrame(raw_data).groupby("id").apply(lambda row: row.shape_pt_lat, axis = 1)

and I am getting this error
TypeError                                 Traceback (most recent call last)
~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
    917             try:
--> 918                 result = self._python_apply_general(f)
    919             except Exception:

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f)
    935         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 936                                                    self.axis)
    937 

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, f, data, axis)
   2272             group_axes = _get_axes(group)
-> 2273             res = f(group)
   2274             if not _is_indexed_like(res, group_axes):

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in f(g)
    907                     with np.errstate(all='ignore'):
--> 908                         return func(g, *args, **kwargs)
    909             else:

TypeError: <lambda>() got an unexpected keyword argument 'axis'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-323-9499f329157a> in <module>()
----> 1 df = pd.DataFrame(raw_data).groupby("id").apply(lambda row: row.shape_pt_lat, axis = 1)

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
    928 
    929                 with _group_selection_context(self):
--> 930                     return self._python_apply_general(f)
    931 
    932         return result

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f)
    934     def _python_apply_general(self, f):
    935         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 936                                                    self.axis)
    937 
    938         return self._wrap_applied_output(

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, f, data, axis)
   2271             # group might be modified
   2272             group_axes = _get_axes(group)
-> 2273             res = f(group)
   2274             if not _is_indexed_like(res, group_axes):
   2275                 mutated = True

~\Miniconda3\envs\data_analysis\lib\site-packages\pandas\core\groupby\groupby.py in f(g)
    906                 def f(g):
    907                     with np.errstate(all='ignore'):
--> 908                         return func(g, *args, **kwargs)
    909             else:
    910                 raise ValueError('func must be a callable if args or '

TypeError: <lambda>() got an unexpected keyword argument 'axis'

I able to get to work if I split the data frame by Id and store it in a list, then loop through each dataframe to extract the values - but I want a way to do it directly using groupby and apply?
Any guidance here would be appreciated?
Thanks! 

A:

I had a similar error. I found that the apply function of a GroupBy object does not behave the same as the apply function of a Pandas DataFrame. More information on the apply function of the GroupBy object can be found here. 
The function you provide in your apply function should get dataframe as an argument. It also returns a dataframe. The function thus modifies a dataframe whereas the function you provided modifies a row. 
It gives the error <lambda>() got an unexpected keyword argument 'axis' as the apply function here only accepts a function which modifies a dataframe and args kwargs which are fed to your function. 
It tries to feed your lambda function the axis parameter (which it thinks is an argument for your function) and since your lambda function does not demand this parameter, it displays this error. 
The solution for your end would be to change the lambda function to a correct function as described above. 

