布尔系列键将重新索引以匹配DataFrame索引 [英] Boolean Series key will be reindexed to match DataFrame index
问题描述
这是我遇到错误的方式:
Here is how I encountered the error:
df.loc[a_list][df.a_col.isnull()]
a_list
的类型是Int64Index
,它包含一个行索引列表.所有这些行索引都属于df
.
The type of a_list
is Int64Index
, it contains a list of row indexes. All of these row indexes belong to df
.
df.a_col.isnull()
部分是我需要过滤的条件.
The df.a_col.isnull()
part is a condition I need for filtering.
如果我分别执行以下命令,则不会收到任何警告:
If I execute the following commands individually, I do not get any warnings:
df.loc[a_list]
df[df.a_col.isnull()]
但是,如果将它们放在一起df.loc[a_list][df.a_col.isnull()]
,则会收到警告消息(但我可以看到结果):
But if I put them together df.loc[a_list][df.a_col.isnull()]
, I get the warning message (but I can see the result):
布尔系列键将重新索引以匹配DataFrame索引
Boolean Series key will be reindexed to match DataFrame index
此错误消息的含义是什么?它会影响返回的结果吗?
What is the meaning of this error message? Does it affect the result that it returned?
推荐答案
尽管有警告,您的方法仍然有效,但是最好不要依赖隐式,不清楚的行为.
Your approach will work despite the warning, but it's best not to rely on implicit, unclear behavior.
解决方案1 ,使a_list
中的索引选择成为布尔掩码:
Solution 1, make the selection of indices in a_list
a boolean mask:
df[df.index.isin(a_list) & df.a_col.isnull()]
解决方案2 ,分两个步骤进行:
df2 = df.loc[a_list]
df2[df2.a_col.isnull()]
解决方案3 ,如果您想要单线,请使用在此处找到的技巧. :
Solution 3, if you want a one-liner, use a trick found here:
df.loc[a_list].query('a_col != a_col')
警告来自以下事实:布尔向量df.a_col.isnull()
是df
的长度,而df.loc[a_list]
是a_list
的长度,即较短.因此,df.a_col.isnull()
中的某些索引不在df.loc[a_list]
中.
The warning comes from the fact that the boolean vector df.a_col.isnull()
is the length of df
, while df.loc[a_list]
is of the length of a_list
, i.e. shorter. Therefore, some indices in df.a_col.isnull()
are not in df.loc[a_list]
.
pandas所做的是在调用数据帧的索引上重新建立布尔系列的索引.实际上,它从df.a_col.isnull()
获取与a_list
中的索引相对应的值.这行得通,但是行为是隐式的,将来很容易改变,因此这就是警告的意思.
What pandas does is reindex the boolean series on the index of the calling dataframe. In effect, it gets from df.a_col.isnull()
the values corresponding to the indices in a_list
. This works, but the behavior is implicit, and could easily change in the future, so that's what the warning is about.
这篇关于布尔系列键将重新索引以匹配DataFrame索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!