Skip to content

Instantly share code, notes, and snippets.

@simon-mo
Created August 29, 2018 20:06
Show Gist options
  • Save simon-mo/f85f448614757f49470f9fc405b4d954 to your computer and use it in GitHub Desktop.
Save simon-mo/f85f448614757f49470f9fc405b4d954 to your computer and use it in GitHub Desktop.

Scenario

Let's say hw1 is released and there is a q1 requires you to sum the dataframe across rows.

Let's Make a Bad Piazza Post

Title: Plz help Tag: ALL TAGS Content:

how to I sum the dataframe in q1?

Let's Make a good Piazza post

Title: How to sum df across row? Tag: hw1, pandas Content:

For the following dataframe, I tried to sum the DataFrame across row.

I looked at the doc and it tells me to use the axis argument. But I'm stuck on what to pass into the argument.

The code and traceback attached.

df = pd.DataFrame(
    {'col1': [1,2,3], 
     'col2':[4,5,6]}
)
df.sum(axis='row')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-d507d436a1fe> in <module>()
----> 1 df.sum(axis='row')

~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in stat_func(self, axis, skipna, level, numeric_only, min_count, **kwargs)
   9591                                       skipna=skipna, min_count=min_count)
   9592         return self._reduce(f, name, axis=axis, skipna=skipna,
-> 9593                             numeric_only=numeric_only, min_count=min_count)
   9594 
   9595     return set_function_name(stat_func, name, cls)

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   6850         else:
   6851             # TODO: Make other agg func handle axis=None properly
-> 6852             axis = self._get_axis_number(axis)
   6853             labels = self._get_agg_axis(axis)
   6854             constructor = self._constructor

~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in _get_axis_number(self, axis)
    373                 pass
    374         raise ValueError('No axis named {0} for object type {1}'
--> 375                          .format(axis, type(self)))
    376 
    377     def _get_axis_name(self, axis):

ValueError: No axis named row for object type <class 'pandas.core.frame.DataFrame'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment