dataframe 两种选择列名的不同,导致结果完全不同。
第1种正常的选择列,输出的是 布尔型series,可以在dataframe中直接使用;
第2种,[ ]中加 [ ] 可以选择多列,其返回值是 布尔型的dataFrame,直接用于筛选出现意外。
构造一个含有np.nan的dataframe,
df4=pd.DataFrame(data=np.arange(0,20).reshape(5,4),columns=['c1','c2','c3','c4'],index=range(0,5))
df4.loc[2,'c2']=np.nan
df4
# 第1种
df4['c2'].isnull()
df4[df4['c2'].isnull()]
对应输出为
0 False
1 False
2 True
3 False
4 False
Name: c2, dtype: bool
c1 c2 c3 c4
2 8 NaN 10 11
# 第2种
df4[['c2']].isnull()
对应输出为
c2
0 False
1 False
2 True
3 False
4 False
Name: c2, dtype: bool
c1 c2 c3 c4
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 NaN NaN NaN NaN
4 NaN NaN NaN NaN
为什么第2种出现这种情况,还没考虑清楚。
探讨了在使用Pandas库进行数据处理时,不同列名选择方式对数据筛选结果的影响。通过对比单列和多列选择的差异,揭示了布尔型Series与DataFrame在实际应用中的行为区别。

7998

被折叠的 条评论
为什么被折叠?



