Pandas: Adding new column to dataframe which is a copy of the index column
<p>I have a dataframe which I want to plot with matplotlib, but the index column is the time and I cannot plot it.</p>

<p>This is the dataframe (df3):</p>

<p><a href="https://i.stack.imgur.com/BhfmS.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/BhfmS.jpg" alt="enter image description here"></a></p>

<p>but when I try the following:</p>

<pre><code>plt.plot(df3['magnetic_mag mean'], df3['YYYY-MO-DD HH-MI-SS_SSS'], label='FDI')
</code></pre>

<p>I'm getting an error obviously:</p>

<pre><code>KeyError: 'YYYY-MO-DD HH-MI-SS_SSS'
</code></pre>

<p>So what I want to do is to add a new extra column to my dataframe (named 'Time) which is just a copy of the index column.</p>

<p>How can I do it?</p>

<p>This is the entire code:</p>

<pre><code>#Importing the csv file into df
df = pd.read_csv('university2.csv', sep=";", skiprows=1)

#Changing datetime
df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'], 
                                               format='%Y-%m-%d %H:%M:%S:%f')

#Set index from column
df = df.set_index('YYYY-MO-DD HH-MI-SS_SSS')

#Add Magnetic Magnitude Column
df['magnetic_mag'] = np.sqrt(df['MAGNETIC FIELD X (μT)']**2 + df['MAGNETIC FIELD Y (μT)']**2 + df['MAGNETIC FIELD Z (μT)']**2)

#Subtract Earth's Average Magnetic Field from 'magnetic_mag'
df['magnetic_mag'] = df['magnetic_mag'] - 30

#Copy interesting values
df2 = df[[ 'ATMOSPHERIC PRESSURE (hPa)',
          'TEMPERATURE (C)', 'magnetic_mag']].copy()

#Hourly Average and Standard Deviation for interesting values 
df3 = df2.resample('H').agg(['mean','std'])
df3.columns = [' '.join(col) for col in df3.columns]

df3.reset_index()
plt.plot(df3['magnetic_mag mean'], df3['YYYY-MO-DD HH-MI-SS_SSS'], label='FDI')  
</code></pre>

<p>Thank you !!</p>