Comparing object columns row wise | Error -ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
Comparing object columns row wise | Error -ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
我正在使用 df['Col1']==df['Col2'] 比较 2 列中的值并得到以下值 error-ValueError: The truth value of a Series is ambiguous.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
复制代码-
if(df['Col1']==df['Col2']):
打印(df.index[df['Col1']])
您正在比较整整两列,但您似乎想要任何不匹配的行的值。让 Pandas 比较所有行的 col1 和 col2 中的值,并使用该比较将 df 过滤为仅不匹配的行。然后您可以获得不匹配行的索引值。或者您可以创建第 3 列并在不匹配时获取第 3 列的所有值。请参阅这个玩具示例。
row1list = ['a', 'a']
row2list = ['b', 'c']
row3list = ['c', 'd']
df = pd.DataFrame([row1list, row2list, row3list], columns=['Col1', 'Col2'])
df_mismatches = df[df['Col1'] != df['Col2']]
df_mismatches = df_mismatches.reset_index() # can put index into its own column if you want
print(df_mismatches)
# index Col1 Col2
# 0 1 b c
# 1 2 c d
我正在使用 df['Col1']==df['Col2'] 比较 2 列中的值并得到以下值 error-ValueError: The truth value of a Series is ambiguous.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
复制代码-
if(df['Col1']==df['Col2']): 打印(df.index[df['Col1']])
您正在比较整整两列,但您似乎想要任何不匹配的行的值。让 Pandas 比较所有行的 col1 和 col2 中的值,并使用该比较将 df 过滤为仅不匹配的行。然后您可以获得不匹配行的索引值。或者您可以创建第 3 列并在不匹配时获取第 3 列的所有值。请参阅这个玩具示例。
row1list = ['a', 'a']
row2list = ['b', 'c']
row3list = ['c', 'd']
df = pd.DataFrame([row1list, row2list, row3list], columns=['Col1', 'Col2'])
df_mismatches = df[df['Col1'] != df['Col2']]
df_mismatches = df_mismatches.reset_index() # can put index into its own column if you want
print(df_mismatches)
# index Col1 Col2
# 0 1 b c
# 1 2 c d