Pandas 使用 0.21.0 对 FutureWarning 进行切片 [英] Pandas slicing FutureWarning with 0.21.0

查看:21
本文介绍了Pandas 使用 0.21.0 对 FutureWarning 进行切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择数据帧子集的子集,只选择一些列,并过滤行.

I'm trying to select a subset of a subset of a dataframe, selecting only some columns, and filtering on the rows.

df.loc[df.a.isin(['Apple', 'Pear', 'Mango']), ['a', 'b', 'f', 'g']]

但是,我收到错误:

Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

现在切片和过滤的正确方法是什么?

What 's the correct way to slice and filter now?

推荐答案

TL;DR:列标题名称中可能存在拼写错误.

这是在 v0.21.1 中引入的更改,并在 文档 全文 -

TL;DR: There is likely a typo or spelling error in the column header names.

This is a change introduced in v0.21.1, and has been explained in the docs at length -

以前,使用标签列表进行选择,其中一个或多个标签丢失总是会成功,为丢失的标签返回 NaN.现在将显示 FutureWarning.在未来,这将提高一个KeyError(GH15747).此警告将在 DataFrameSeries 用于在传递带有 at 的标签列表时使用 .loc[][[]]至少缺少 1 个标签.

Previously, selecting with a list of labels, where one or more labels were missing would always succeed, returning NaN for missing labels. This will now show a FutureWarning. In the future this will raise a KeyError (GH15747). This warning will trigger on a DataFrame or a Series for using .loc[] or [[]] when passing a list-of-labels with at least 1 missing label.

例如

df

     A    B  C
0  7.0  NaN  8
1  3.0  3.0  5
2  8.0  1.0  7
3  NaN  0.0  3
4  8.0  2.0  7

在你正在做的时候尝试某种切片 -

Try some kind of slicing as you're doing -

df.loc[df.A.gt(6), ['A', 'C']]

     A  C
0  7.0  8
2  8.0  7
4  8.0  7

没问题.现在,尝试用不存在的列标签替换 C -

No problem. Now, try replacing C with a non-existent column label -

df.loc[df.A.gt(6), ['A', 'D']]
FutureWarning: Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.
     
     A   D
0  7.0 NaN
2  8.0 NaN
4  8.0 NaN

因此,在您的情况下,错误是因为您传递给 loc 的列标签.再看看它们.

So, in your case, the error is because of the column labels you pass to loc. Take another look at them.

这篇关于Pandas 使用 0.21.0 对 FutureWarning 进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆