Pandas:向DataFrame添加新列,它是索引列的副本

uinbv5nw  于 2022-09-21  发布在  其他
关注(0)|答案(3)|浏览(154)

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

这是 Dataframe (DF3):

但当我尝试以下操作时:

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

我显然收到了一个错误:

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

因此,我想要做的是向我的 Dataframe (名为‘time)中添加一个新的额外列,它只是索引列的副本。

我该怎么做呢?

以下是完整的代码:


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

谢谢你!!

huwehgph

huwehgph1#

我认为您需要reset_index

df3 = df3.reset_index()

可能的解决方案,但我认为inplace不是很好的做法,检查这个和this

df3.reset_index(inplace=True)

但是,如果您需要新的专栏,请使用:

df3['new'] = df3.index

我认为你可以做得更好:

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

然后省略:


# 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')
0ve6wy6x

0ve6wy6x2#

您可以直接访问索引并将其打印出来,以下是一个示例:

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

# Get index in vertiacal axis

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

mepcadol3#

您还可以使用eval来实现以下目标:

In [2]: df = pd.DataFrame({'num': range(5), 'date': pd.date_range('2022-06-30', '2022-07-04')}, index=list('ABCDE'))

In [3]: df
Out[3]: 
   num       date
A    0 2022-06-30
B    1 2022-07-01
C    2 2022-07-02
D    3 2022-07-03
E    4 2022-07-04

In [4]: df.eval('index_copy = index')
Out[4]: 
   num       date index_copy
A    0 2022-06-30          A
B    1 2022-07-01          B
C    2 2022-07-02          C
D    3 2022-07-03          D
E    4 2022-07-04          E

相关问题