Warm tip: This article is reproduced from serverfault.com, please click

python-不再支持将类似列表的标签传递给.loc或[]且缺少任何标签

(python - Passing list-likes to .loc or [] with any missing labels is no longer supported)

发布于 2020-04-18 15:34:14

我想用指定的列创建一个修改的数据框。我尝试了以下操作,但抛出错误“不再支持将缺少列表的标签传递给.loc或[]并且缺少任何标签”

# columns to keep
filtered_columns = ['text', 'agreeCount', 'disagreeCount', 'id', 'user.firstName', 'user.lastName', 'user.gender', 'user.id']
tips_filtered = tips_df.loc[:, filtered_columns]

# display tips
tips_filtered

谢谢

Questioner
swasthik nayak
Viewed
0
Joel Oduro-Afriyie 2020-11-17 07:10:37

pandas 似乎已弃用这种索引方法。根据他们的文档

不建议使用此行为,它将显示一条警告消息,指向此部分。推荐的替代方法是使用.reindex()

使用推荐的新方法,你可以使用以下方法过滤列:

tips_filtered = tips_df.reindex(columns = filtered_columns)

注意:要为行重新索引,你可以使用reindex(index = ...)此处有更多信息)。