Python KeyError:“传递列表喜欢。不再支持带有任何缺少标签的loc或[]

Python KeyError:“传递列表喜欢。不再支持带有任何缺少标签的loc或[],python,reindex,Python,Reindex,我有一个包含以下列的数据框: 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

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

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')
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')```
我只想从数据框中选择以下列:

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)
类似地,我有一个包含以下列的数据框:

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

这是什么原因


提前谢谢你

因为在第一个示例中,您引入了原始数据框ex:Job.ID_列表中不存在的列名

在第二个示例中,所有列都已位于原始数据帧中


正如错误所说:“传递列表喜欢.loc或[]以及任何缺少的标签…..

我现在看到了它。我没有意识到这一点。非常感谢你!