Reputation: 31
Let's say I had a dataframe:
df = pd.DataFrame([[3, 2, 1], [5, 4, 2]])
3 2 1
5 4 2
I want to return a dataframe that has the percent change from one column to the next. So the above dataset would return:
.666 .5
.8 .5
How would I accomplish this in pandas?
Upvotes: 0
Views: 185
Reputation: 2750
Not a very ideal way but works.
df['diff_1_0']=abs(df[0]-df[1])/df[0]
df['diff_2_1']=abs(df[1]-df[2])/df[1]
df=df[['diff_1_0','diff_2_1']]
If you have a lot of columns you can make it into a loop like below.
column_count=df.shape[1]+1
column_names=df.columns
for columns in df:
if columns==0:
continue
else:
df.insert(columns, str('diff_'+str(column_count)), abs(df[columns-1]-df[columns])/df[columns-1])
column_count=column_count+1
df=df.drop(column_names, axis=1)
Hope it helps!
Upvotes: 0
Reputation: 520
Pandas has a function for this. Use pct_change:
df.pct_change(axis='columns')
This will output the 'percentage change' (per your question) from one column to other. Not the division of one column to the other (seems to be your example).
Upvotes: 1