martina.physics
martina.physics

Reputation: 9804

Pandas plot DataFrame rolling mean gives weird result

If I have a DataFrame built as

df = pd.DataFrame({'a': [i for i in range(10)],  
                   'b': [2*i for i in range(10)]})

so that its plot of 'b' against 'a' (obtained via df.plot('a', 'b')) is simply a straight line, and I compute the running mean of it as

rm_df = df.rolling(window=2).mean()

and try to plot again 'b' against 'a', I get the reported figure, where the x axis is giving weird values.

weird values in rolling mean df plotting

I've checked by looking at the dataframe that all values are good, so what is happening?

Upvotes: 0

Views: 1794

Answers (1)

sgDysregulation
sgDysregulation

Reputation: 4417

Use the following code:

import pandas as pd

df = pd.DataFrame({'a': [i for i in range(10)],  
                   'b': [2*i for i in range(10)]})

rm_df = df.rolling(window=2).mean().plot(
          kind='line', x='a',y='b', use_index=False)


Setting use_index to false will allow you to specify the columns to be used as x and y

The result will look like this enter image description here

Upvotes: 1

Related Questions