Menu Close

How to add new column to dataframe which is a copy of the index column with Python Pandas?

To add new column to dataframe which is a copy of the index column with Python Pandas, we can call plot with the index.

For instance, we write

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))

plt.plot(df.index, df[0])
plt.show()

to call plt.plot with the index for the horizontal axis with df.index and plot them as values.

Posted in Python, Python Answers