Error"can Only Compare Identically-labeled Series Objects" And Sort_index
I have two dataframes df1 df2with the same numbers of rows and columns and variables, and I'm trying to compare the boolean variable choice in the two dataframes. Then use if/else
Solution 1:
I think you need reset_index
for same index values and then comapare - for create new column is better use mask
or numpy.where
:
Also instead +
use |
because working with booleans.
df1 = df1.reset_index(drop=True)
df2 = df2.reset_index(drop=True)
df1['v_100'] = df1['choice'].mask(df1['choice'] != df2['choice'],
(df1['choice'] + df2['choice']) *0.5)
df1['v_100'] = np.where(df1['choice'] != df2['choice'],
(df1['choice'] | df2['choice']) *0.5,
df1['choice'])
Samples:
print (df1)
v_100 choice
57True60True77False82Trueprint (df2)
v_100 choice
41False52True674True76True
df1 = df1.reset_index(drop=True)
df2 = df2.reset_index(drop=True)
print (df1)
v_100 choice
07True10True27False32Trueprint (df2)
v_100 choice
01False12True274True36True
df1['v_100'] = df1['choice'].mask(df1['choice'] != df2['choice'],
(df1['choice'] | df2['choice']) * 0.5)
print (df1)
v_100 choice
00.5True11.0True20.5False31.0True
Solution 2:
The error happens because you compare two pandas.Series objects with different indices. A simple solution would be to compare just the values in the series. Try it:
if df1['choice'].values!= df2['choice'].values
Post a Comment for "Error"can Only Compare Identically-labeled Series Objects" And Sort_index"