paulchen paulchen - 11 months ago 94
Python Question

Use index in pandas to plot data

I have a pandas-Dataframe and use

to calculate means (e.g. daily or monthly means).
Here is a small example.

import pandas as pd
import numpy as np

dates = pd.date_range('1/1/2000', periods=100)
df = pd.DataFrame(np.random.randn(100, 1), index=dates, columns=['A'])

monthly_mean = df.resample('M', how='mean')

How do I plot the monthly_mean now?
How do I manage to use the index of my new created DataFrame
as the x-axis?
Thanks in advance.

Answer Source

You can use reset_index to turn the index back into a column:

monthly_mean.reset_index().plot(x='index', y='A')