问题描述pandas实际开发中进行运算时的一个坑!import pandas as pd
v = {'value': 'a'}
d = [{'name': 'abc', 'age': 10}, {'name': None, 'age': 11}, {'name': 'def', 'age': 9}]
df = pd.DataFrame(d)
new_1 = df[(df['age'] >= 10) | df['name'].str.contains(v['value'])]
new_2 = df[df['name'].str.contains(v['value']) | (df['age'] >= 10)]
print('-'*80)
print(df)
print('-'*80)
print(new_1)
print('-'*80)
print(new_2)输出--------------------------------------------------------------------------------
age name0 10 abc1 11 None2 9 def-------------------------------------------------------------------------------- age name0 10 abc1 11 None-------------------------------------------------------------------------------- age name0 10 abc想请问为什么 new1 和 new2的结果不一样?
1 回答
慕仙森
TA贡献1827条经验 获得超7个赞
>>> df.dtypes age int64 name object dtype: object>>> (df['age'] >= 10)0 True1 True2 FalseName: age, dtype: bool>>> df['name'].str.contains(v['value'])0 True1 None # 注意这个2 FalseName: name, dtype: object>>> (df['age'] >= 10) | df['name'].str.contains(v['value'])0 True1 True2 Falsedtype: bool>>> df['name'].str.contains(v['value']) | (df['age'] >= 10)0 True1 False2 Falsedtype: bool
None 是python中的对象,不是pandas的数据类型,以python的规则进行运算
把None改为''
.
>>> df2 = pd.DataFrame([{'name': 'abc', 'age': 10}, {'name': '', 'age': 11}, {'name': 'def', 'age': 9}])>>> df2.dtypes age int64 name object dtype: object>>> (df2['age'] >= 10)0 True1 True2 FalseName: age, dtype: bool>>> df2['name'].str.contains(v['value'])0 True1 False2 FalseName: name, dtype: bool>>> (df2['age'] >= 10) | df2['name'].str.contains(v['value'])0 True1 True2 Falsedtype: bool>>> df2['name'].str.contains(v['value']) | (df2['age'] >= 10)0 True1 True2 Falsedtype: bool>>>
- 1 回答
- 0 关注
- 474 浏览
添加回答
举报
0/150
提交
取消