【发布时间】:2017-11-30 01:53:15
【问题描述】:
我有两个数据框 df1 df2 具有相同的行数和列数以及变量,我正在尝试比较两个数据框中的布尔变量 choice。然后使用if/else 来操作数据。但是当我尝试比较布尔变量时似乎有些错误。
这是我的数据框示例和代码:
#df1
v_100 choice #boolean
7 True
0 True
7 False
2 True
#df2
v_100 choice #boolean
1 False
2 True
74 True
6 True
def lastTwoTrials_outcome():
df1 = df.iloc[5::6, :] #df1 and df2 are extracted from the same dataframe first
df2 = df.iloc[4::6, :]
if df1['choice'] != df2['choice']: # if "choice" is different in the two dataframes
df1['v_100'] = (df1['choice'] + df2['choice']) * 0.5
这是错误:
if df1['choice'] != df2['choice']:
File "path", line 818, in wrapper
raise ValueError(msg)
ValueError: Can only compare identically-labeled Series objects
我发现了同样的错误here,答案首先建议sort_index,但我真的不明白为什么?谁能详细解释一下(如果这是正确的解决方案)?
谢谢!
【问题讨论】:
标签: python pandas indexing boolean