Search code examples
excelpandasdataframekeyerror

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


I get a keyerror for "Displacement" when I try to plot Force against Displacement with pandas for these group of dataframes. Please help.

The link to the excel sheet being used: https://www.dropbox.com/s/f8lnp973ojv3ish/neurospheress.xlsx?dl=0

I tried clearing any space in the column titles but it doesn't work

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

Solution

  • The below solution works, it basically adds two things to your solution

    a) Skip the first row from excel b) Rename the column names for df2 and 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()