Pandas: adding a new column to the dataframe, which is a copy of the index column

I have a dataframe that I want to build using matplotlib, but the index column is the time, and I cannot build it.

This is dataframe (df3):

enter image description here

but when I try the following:

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

I get an error message:

 KeyError: 'YYYY-MO-DD HH-MI-SS_SSS' 

So what I want to do is add a new extra column to my framework (named "Time"), which is just a copy of the index column.

How can i do this?

This is all the 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 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') 

Thanks!

+5
source share
2 answers

I think you need reset_index .

 df3.reset_index(inplace=True) 

Or:

 df3 = df3.reset_index() 

But if you need a new column, use:

 df3['new'] = df3.index 

I think you can read_csv better:

 df = pd.read_csv('university2.csv', sep=";", skiprows=1, index_col='YYYY-MO-DD HH-MI-SS_SSS', parse_dates='YYYY-MO-DD HH-MI-SS_SSS') #if doesnt work, use pd.to_datetime 

And then omit:

 #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') 
+5
source

You can directly access the index and get it on the chart, here is an example:

 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)) #Get index in horizontal axis plt.plot(df.index, df[0]) plt.show() 

enter image description here

  #Get index in vertiacal axis plt.plot(df[0], df.index) plt.show() 

enter image description here

+1
source

All Articles