In [1]:
import pandas as pd
In [2]:
df = pd.DataFrame({'a': range(1, 6),
'b': [x**2 for x in range(1, 6)],
'c': [x**3 for x in range(1, 6)]})
In [3]:
print(df)
In [4]:
print(df.pct_change())
In [5]:
print(df.pct_change(2))
In [6]:
print(df.pct_change(-1))
In [7]:
print(df.pct_change(axis=1))
In [8]:
print(df.pct_change(-1, axis=1))
In [9]:
print(df.pct_change(2).dropna())
In [10]:
print(df.pct_change(2).fillna(0))
In [11]:
print(df.pct_change(2).fillna(method='bfill'))
In [12]:
df['b_pct_change'] = df['b'].pct_change(-1)
print(df)