rstriker
rstriker

Reputation: 45

Python Pandas subtracting each column and creating new column

I have a pandas dataframe with the following columns:

January, February, March, April

I want to subtract February with January. March with February. April with March etc. Then for each of these calculations a new column to be created with the calculation.

I have tried creating a for loop, but not it did not work as I am not certain how to store each calculation into a new column and how to state February - previous column etc.

Any help will be much appreciated.

Dataframe example:

Sales January February March April 
Team1 1000    2000     3000  4000
Team2 1000    2000     3000  4000

Dataframe result:

Sales January diff February diff March  diff   April 
Team1 1000    1000 2000     1000 3000    1000  4000
Team2 1000    1000 2000     1000 3000    1000  4000

Upvotes: 4

Views: 526

Answers (1)

Code Different
Code Different

Reputation: 93181

Try this:

df = pd.concat([
    df,
    df.loc[:, 'January':] \
        .diff(axis=1) \
        .drop(columns='January') \
        .add_suffix('_diff')    
], axis=1)

Try this:

df = pd.concat([
    df,
    df.loc[:, 'January':] \
        .diff(axis=1) \
        .drop(columns='January') \
        .add_suffix('_diff')
], axis=1)

What it does:

  • df.loc[:, 'January':] takes all columns from January and to the right
  • .diff(axis=1) calculate the difference with the previous column
  • .drop(columns='January') drops the difference column for Jan since it's all n/a
  • .add_suffix('_diff') renames February to February_diff and so on

The outer concat is to add the diff columns back to your original dataframe

Upvotes: 1

Related Questions