1

here I have a pandas Dataframe df like:

     A    B    C
 0   1    2    3
 1   1    2    3
 3   1    2    3

Then I have a list

x=['B','C']

I want to get the sum of each number of each row under B & C columns. So I write like:

df[x].sum(axis=1).values

However, I get an error

TypeError: f() got an unexpected keyword argument 'axis'

I don't understand why this will give an error. My code is run in ipython notebook. Could you give any suggestions? Thanks.

UPDATE: the real df is like:

                  Date      Ayotte  Hassan
Date                                 
2016-06-29     2016-06-29    46.8    45.3
2016-06-30     2016-06-30    46.8    45.3
2016-07-01     2016-07-01    46.8    45.3
2016-07-02     2016-07-02    46.8    45.3
2016-07-03     2016-07-03    46.8    45.3
2016-07-04     2016-07-04    46.8    45.3
2016-07-20     2016-07-20    45.8    45.2
2016-07-21     2016-07-21    45.8    45.2
2016-07-22     2016-07-22    45.8    45.2
   ...            ...         ...     ...
2016-10-09     2016-10-09    48.0    44.5
2016-10-10     2016-10-10    48.0    44.5
2016-10-11     2016-10-11    46.7    44.7
2016-10-16     2016-10-16    46.3    44.0
2016-10-17     2016-10-17    46.3    44.0
2016-10-18     2016-10-18    46.0    44.3
2016-10-19     2016-10-19    45.7    45.3
2016-10-20     2016-10-20    44.0    46.0
2016-10-21     2016-10-21    44.0    46.0
2016-10-22     2016-10-22    44.0    46.0
2016-10-23     2016-10-23    44.0    46.0

The dtypes of df is

Date      datetime64[ns]
Ayotte           float64
Hassan           float64
dtype: object

Then, I did

df = df.resample('D')

The df shown above is the data before resample. The list x is

x=['Ayotte','Hassan']

Then the error comes when I run this code

print df[x].sum(axis=1).values
13
  • Did you mean to have your index go 0, 1, 3 in your DataFrame? Because I did the exact same thing (with the proper indicies) and it works. Commented Nov 18, 2016 at 2:08
  • @pshep123 that won't matter Commented Nov 18, 2016 at 2:09
  • .values is the issue Edit, I take that back....It works either way for me Commented Nov 18, 2016 at 2:15
  • @pshep123 My df's index is the date like 2016-11-18. Commented Nov 18, 2016 at 2:18
  • @anshanno I tried without .values, but still get the same error. Commented Nov 18, 2016 at 2:19

2 Answers 2

1

It's hard to diagnose w/o a continuous example that demonstrate the error.

If I start out with:

import numpy
import pandas
df = pandas.DataFrame(
    numpy.arange(9).reshape(3, 3),
    index=['a', 'b', 'c'],
    columns=['X', 'Y', 'Z']
)
print(df)

Which gives:

   X  Y  Z
a  0  1  2
b  3  4  5
c  6  7  8

I can then do:

df[['X', 'Y']].sum(axis=1)

To get:

a     1
b     7
c    13
dtype: int64
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply. I also try something myself and there is no error. My actual dataframe df has three columns: date, person1, person2. The index of df is also date. The date is like 2016-11-18. Under person1/2, there are float64 numbers. when I try to calculate the sum as above, then I will get the error.
0

In:

,Date,Ayotte,Hassan
2016-06-29,2016-06-29,46.8,45.3
2016-06-30,2016-06-30,46.8,45.3
2016-07-01,2016-07-01,46.8,45.3
2016-07-02,2016-07-02,46.8,45.3
2016-07-03,2016-07-03,46.8,45.3
2016-07-04,2016-07-04,46.8,45.3
2016-07-20,2016-07-20,45.8,45.2

Code:

import pandas as pd

df = pd.read_csv('file.txt', index_col=0)
df['Total'] = df[['Ayotte', 'Hassan']].sum(axis=1)

print df

Out:

                  Date  Ayotte  Hassan  Total
2016-06-29  2016-06-29    46.8    45.3   92.1
2016-06-30  2016-06-30    46.8    45.3   92.1
2016-07-01  2016-07-01    46.8    45.3   92.1
2016-07-02  2016-07-02    46.8    45.3   92.1
2016-07-03  2016-07-03    46.8    45.3   92.1
2016-07-04  2016-07-04    46.8    45.3   92.1
2016-07-20  2016-07-20    45.8    45.2   91.0

3 Comments

Thanks. I tried this but still get the same error. However, I found if I remove the line df = df.resample('D'), then it will work. Do you have any idea about that?
@Mr_Pi not sure exactly, I have never used resample. Are you using that resample just to get the date into Year-Month-Day? From the docs it looks like you are supposed to call it on a per series basis series.resample(). But if all you want is the date in Y-m-d you should be using something like df['in_day'] = df.my_datetime.dt.strftime('%Y-%m-%d')
resample doesn't return a dataframe. it returns a grouping object, you need to aggregate the resample object before treating it like a normal dataframe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.