gpt4 book ai didi

python - pd.DataFrame.all 中切换的 axis 参数的含义

转载 作者:行者123 更新时间:2023-12-01 05:00:35 25 4
gpt4 key购买 nike

我有以下数据框:

>>> df = pd.DataFrame([[True, np.nan, False],[True,np.nan,False],[True,np.nan,False]])
>>> df
0 1 2
0 True NaN False
1 True NaN False
2 True NaN False

根据docs ,执行 df.all(axis=1,skipna=True) 对应于检查所有值是否按列为 true,因此我希望它给出 True,True,False,但它给出 False,False,False。看来axis的含义颠倒了,axis=0是columnwise的。

这似乎与 DataFrame.dropna 中 axis 的含义相矛盾,例如,

>>> df.dropna(axis=1)
0 2
0 True False
1 True False
2 True False

以及 np.delete .

这是故意的吗?如果是这样,为什么?

最佳答案

我认为这是文档中的一个错误,因为此方法将调用 numpy.all 如果您比较输出,它们是相同的:

In [211]:

np.all(df,axis=0)
Out[211]:
array([True, nan, False], dtype=object)
In [212]:

np.all(df, axis=1)
Out[212]:
array([False, False, False], dtype=object)

此外,dropnanp.delete 同意输出:

In [213]:

df.dropna(axis=1)
Out[213]:
0 2
0 True False
1 True False
2 True False
In [222]:

np.delete(df.values, 1,axis=1)
Out[222]:
array([[True, False],
[True, False],
[True, False]], dtype=object)

关于python - pd.DataFrame.all 中切换的 axis 参数的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26295422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com