我在Pandas中使用布尔索引.问题是为什么声明:
a[(a['some_column']==some_number) & (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
工作正常,而
a[(a['some_column']==some_number) and (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
存在错误?
例:
a=pd.DataFrame({'x':[1,1],'y':[10,20]})
In: a[(a['x']==1)&(a['y']==10)]
Out: x y
0 1 10
In: a[(a['x']==1) and (a['y']==10)]
Out: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud) 刚刚进入 Python,所以希望我不会在这里问一个愚蠢的问题......
所以我有一个名为“df_complete”的 Pandas 数据框,假设有 100 行,包含名为:“type”、“writer”、“status”、“col a”、“col c”的列。我想创建/更新一个名为“temp_df”的新数据框,并根据使用“df_complete”值的条件创建它。
temp_df = pandas.DataFrame()
if ((df_complete['type'] == 'NDD') & (df_complete['writer'] == 'Mary') & (df_complete['status'] != '7')):
temp_df['col A'] = df_complete['col a']
temp_df['col B'] = 'good'
temp_df['col C'] = df_complete['col c']
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我收到以下错误消息:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud)
我阅读了这个线程并将我的“和”更改为“&”: 系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
我还在这里阅读了此线程以将所有内容放在括号中:将 dtyped [float64] 数组与 Pandas DataFrame 中类型为 [bool] 的标量进行比较
但是错误仍然存在。这是什么原因造成的?我该如何解决?
** 后续问题 ** 另外,如何获取满足条件的行的索引值?