Reputation: 45
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
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 onThe outer concat
is to add the diff columns back to your original dataframe
Upvotes: 1