Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *