尝试使用 Pandas 绘制这些数据帧组时出现 KeyError

I get a KeyError when trying to plot these group of dataframes with Pandas

当我尝试用 pandas 为这组数据帧绘制力与位移的对比图时,出现 "Displacement" 的键盘错误。请帮忙。

link到excelsheet正在使用: https://www.dropbox.com/s/f8lnp973ojv3ish/neurospheress.xlsx?dl=0

我尝试清除列标题中的任何 space,但它不起作用

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_excel('neurospheress.xlsx', sep='\s*,\s*', sheet_name = 'LS')

df1 = data.iloc[:80,:2]
df2 = data.iloc[:80,2:4]
df3 = data.iloc[:80,4:]
dfs = [df1,df2,df3]

for i,df in enumerate(dfs):
    plt.plot(df['Displacement'], df['Force'], linestyle='--', alpha= 0.8, label='df{}'.format(i))
plt.legend(loc='best')
plt.show()

下面的解决方案有效,它基本上为您的解决方案添加了两件事

a) 跳过 excel 的第一行 b) 重命名 df2 和 df3

的列名
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_excel('neurospheress.xlsx', sep='\s*,\s*', sheet_name = 'LS',skiprows=1)

df1 = data.iloc[:80,:2]
df2 = data.iloc[:80,2:4]
df3 = data.iloc[:80,4:]
dfs = [df1,df2,df3]

df2.rename(columns={'Force.1':'Force','Displacement.1':'Displacement'},inplace=True)
df3.rename(columns={'Force.2':'Force','Displacement.2':'Displacement'},inplace=True)

print(data.columns)
print(df1.columns)
print(df2.columns)

for i,df in enumerate(dfs):
    plt.plot(df['Displacement'], df['Force'], linestyle='--', alpha= 0.8, label='df{}'.format(i))
plt.legend(loc='best')
plt.show()