pandas 用0.21.0切片FutureWarning [英] Pandas slicing FutureWarning with 0.21.0
问题描述
我试图选择一个数据框的一个子集的一个子集,只选择一些列,然后对行进行过滤.
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?
推荐答案
This is a change introduced in v0.21.1
, and has been explained in the docs at length -
以前,选择带有标签列表的标签,其中一个或多个标签 丢失总是可以成功的,为丢失的标签返回
NaN
. 现在将显示FutureWarning
.将来这将引发KeyError
( GH15747 ).此警告将在DataFrame
或Series
用于在传递带有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 aFutureWarning
. In the future this will raise aKeyError
(GH15747). This warning will trigger on aDataFrame
or aSeries
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屋!