Python Pandas:向数据框添加新列,这是索引列的副本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36932759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:34:25  来源:igfitidea点击:

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

pythonpandasmatplotlib

提问by ValientProcess

I have a dataframe which I want to plot with matplotlib, but the index column is the time and I cannot plot it.

我有一个数据框,我想用 matplotlib 绘制它,但索引列是时间,我无法绘制它。

This is the dataframe (df3):

这是数据框(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'm getting an error obviously:

我显然收到一个错误:

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

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.

所以我想要做的是向我的数据框(名为“时间”)添加一个新的额外列,它只是索引列的副本。

How can I do it?

我该怎么做?

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

Thank you !!

谢谢 !!

回答by jezrael

I think you need reset_index:

我认为你需要reset_index

df3 = df3.reset_index()

Possible solution, but I think inplaceis not good practice, check thisand this:

可能的解决方案,但我认为inplace这不是一个好习惯,检查这个这个

df3.reset_index(inplace=True)

But if you need new column, use:

但如果您需要新列,请使用:

df3['new'] = df3.index

I think you can read_csvbetter:

我认为你可以read_csv更好:

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')

回答by Abbas

You can directly access in the index and get it plotted, following 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

在此处输入图片说明