【问题标题】:Why does the lookup by index value result in a key errorWhy does the lookup by index value result in a key error
【发布时间】:2022-12-01 20:42:09
【问题描述】:

The code:

df = pd.DataFrame({
    'MNumber':['M03400001','M00000021','M10450001','M00003420','M02635915','M51323275','M63061229','M63151022'],
    'GPA':[3.01, 4.00, 2.95, 2.90, 3.50, 3.33, 2.99, 3.98],
    'major':['IS','BANA','IS','IS','IS','BANA','IS', 'BANA'],
    'internship':['P&G', 'IBM', 'P&G', 'IBM', 'P&G', 'EY','EY', 'Great American'],
    'job_offers':[2,0,0,3,2,1,4,3],
    'graduate_credits':[5,1,0,5,2,2,3,4]
})
x  =  df.groupby('internship').describe()
#print(x.info())
print(x["IBM"])

The error:

KeyError: 'IBM'

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    x['IBM'] tries to access thecolumn'IBM', which does not exist.
    x.loc['IBM', :] accesses the row 'IBM', which does exist.

    【讨论】:

      【解决方案2】:

      Your dataframe x does not contain a key called IBM.

      Try

      print(x)
      

      To see the keys contained in your dataframe. Even better, you can try

      print(x.columns)
      

      To see the available columns of your dataframe.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-02
        • 2013-09-30
        • 1970-01-01
        • 2022-12-26
        • 2022-12-19
        • 2022-12-19
        • 2022-12-01
        • 2022-12-01
        相关资源
        最近更新 更多