aabujamra
aabujamra

Reputation: 4636

Python/Pandas - Summing DataFrame columns items

I have the following DataFrame:

            Value   Seasonal
Date                        
2004-01-01      0 -10.000000
2004-02-01    173 -50.000000
2004-03-01    225   0.000000
2004-04-01    230   9.000000

I want to sum its items so it gets like this:

                   Value 
Date                        
2004-01-01    -10.000000
2004-02-01    123.000000
2004-03-01    225.000000
2004-04-01    239.000000

Is there an easy way to do that?

Upvotes: 1

Views: 94

Answers (3)

2342G456DI8
2342G456DI8

Reputation: 1809

df['Value'] += df['Seasonal']
dfnew = df.drop('Seasonal', axis=1)
print(dfnew)

Output:
Date  Value
0  2004-01-01    -10
1  2004-02-01    123
2  2004-03-01    225
3  2004-04-01    239

Upvotes: 0

Eli Korvigo
Eli Korvigo

Reputation: 10493

If you want to create an entirely new data frame without messing up the old one.

import pandas as pd

In [12]: pd.DataFrame({"Date": df["Date"], "Value": df["Value"] + df["Seasonal"]})
Out[12]: 
         Date  Value
0  2004-01-01    -10
1  2004-02-01    123
2  2004-03-01    225
3  2004-04-01    239

Upvotes: 2

Fabio Lamanna
Fabio Lamanna

Reputation: 21552

You can just:

df['Value'] = df['Value'] + df['Seasonal']

Upvotes: 1

Related Questions