Python Pandas 使用 0.21.0 对 FutureWarning 进行切片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47896240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas slicing FutureWarning with 0.21.0
提问by QuinRiva
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']]
However, I'm getting the error:
但是,我收到错误:
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?
现在切片和过滤的正确方法是什么?
采纳答案by cs95
This is a change introduced in v0.21.1
, and has been explained in the docsat length -
这是 中引入的更改v0.21.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.
以前,使用标签列表进行选择,其中缺少一个或多个标签总是会成功,并返回
NaN
缺少标签。这现在将显示一个FutureWarning
. 将来这将引发KeyError
(GH15747)。此警告将在 aDataFrame
或 a 上触发,Series
用于使用.loc[]
或[[]]
传递至少缺少 1 个标签的标签列表。
For example,
例如,
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
No problem. Now, try replacing C
with a non-existent column label -
没问题。现在,尝试用C
不存在的列标签替换-
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
So, in your case, the error is because of the column labels you pass to loc
. Take another look at them.
因此,在您的情况下,错误是由于您传递给loc
. 再看看他们。
回答by Shital Shah
This error also occurs with .append
call when the list contains new columns. To avoid this
.append
当列表包含新列时,调用也会发生此错误。为避免这种情况
Use:
用:
df=df.append(pd.Series({'A':i,'M':j}), ignore_index=True)
Instead of,
代替,
df=df.append([{'A':i,'M':j}], ignore_index=True)
Full error message:
完整的错误信息:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:1472: FutureWarning: Passing list-likes to .loc or with any missing label will raise KeyError in the future, you can use .reindex() as an alternative.
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:1472:FutureWarning:将类似列表传递给 .loc 或缺少任何标签将在将来引发 KeyError,您可以使用 .reindex( ) 作为备选。
回答by Max Vinogradov
Sorry, I'm not sure that I correctly understood you, but seems that next way could be acceptable for you:
抱歉,我不确定我是否正确理解了您,但您似乎可以接受下一种方式:
df[df['a'].isin(['Apple', 'Pear', 'Mango'])][['a', 'b', 'f', 'g']]
df[df['a'].isin(['Apple', 'Pear', 'Mango'])][['a', 'b', 'f', 'g']]
Snippet description:
片段说明:
df['a'].isin(['Apple', 'Pear', 'Mango']) # it's "filter" by data in each row in column *a*
df[['a', 'b', 'f', 'g']] # it's "column filter" that provide ability select specific columns set