【问题标题】:KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supportedKeyError:'不再支持将列表喜欢传递给带有任何缺失标签的 .loc 或 []
【发布时间】:2021-03-05 14:26:50
【问题描述】:

我有一个包含以下列的数据框:

job_post.columns 

Index(['Job.ID_list', 'Provider', 'Status', 'Slug', 'Title', 'Position',
   'Company', 'City', 'State.Name', 'State.Code', 'Address', 'Latitude',
   'Longitude', 'Industry', 'Job.Description', 'Requirements', 'Salary',
   'Listing.Start', 'Listing.End', 'Employment.Type', 'Education.Required',
   'Created.At', 'Updated.At', 'Job.ID_desc', 'text'],
  dtype='object')

我只想从数据框中选择以下列:

columns_job_post = ['Job.ID_listing', 'Slug', 'Position', 'Company', 'Industry', 'Job.Description','Employment.Type', 'Education.Required', 'text'] # columns to keep

但是,我得到了结果:

KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported

我通过写作解决了这个问题:

jobs_final = job_post.reindex(columns = columns_job_post)

同样,我有一个包含以下列的数据框:

cand_exp.columns

Index(['Applicant.ID', 'Position.Name', 'Employer.Name', 'City', 'State.Name',
   'State.Code', 'Start.Date', 'End.Date', 'Job.Description', 'Salary',
   'Can.Contact.Employer', 'Created.At', 'Updated.At'],
  dtype='object')```

我还使用 .loc 从整个列表中仅选择了 一些 列,但我没有得到 KeyError: Passing list-like...

columns_cand_exp = ['Applicant.ID', 'Position.Name', 'Employer.Name', 'Job.Description', 'Salary']``` # columns to keep

resumes_final = cand_exp.loc[:, columns_cand_exp]

这是什么原因?

提前谢谢你!

【问题讨论】:

    标签: python reindex


    【解决方案1】:

    因为在第一个示例中,您引入的列名不存在于原始数据框中(例如:Job.ID_listing)。

    在第二个示例中,所有列已经在原始数据框中。

    正如错误所说:'将列表喜欢传递给带有任何缺失标签的 .loc 或 [] .....

    【讨论】:

    • 我现在看到了。我没有意识到这一点。非常感谢!
    猜你喜欢
    • 2021-07-21
    • 2021-07-18
    • 2020-07-06
    • 2020-12-24
    • 2021-10-23
    • 2021-04-24
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多