Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas.errors.MergeError Not allowed to merge between different levels

Tags:

python

pandas

I'm having trouble joining two datasets

data = races_df.join(runs_df, on='race_id', how='right')
X = data[data.columns[:-14]]
ss = preprocessing.StandardScaler()
X = pd.DataFrame(ss.fit_transform(X),columns = X.columns)
y_won = data[data.columns[-14:]].applymap(lambda x: 1.0 if 0.5 < x < 1.5 else 0.0)
    
print(X.shape)
print(y_won.shape)

when i run the code i get error message

pandas.errors.MergeError: Not allowed to merge between different levels. 
   (1 levels on the left, 2 on the right)

Does anyone know the correct code that wont produce this error thx

like image 338
user3060111 Avatar asked Dec 04 '25 23:12

user3060111


1 Answers

In second DataFrame is MultiIndex, so is possible remove first or second level, depends of data:

data = t1_df.join(t2_df.droplevel(0), on='race_id')
data = t1_df.join(t2_df.droplevel(1), on='race_id')

Or convert it to column:

data = t1_df.join(t2_df.reset_index(0), on='race_id')
data = t1_df.join(t2_df.reset_index(1), on='race_id')
like image 98
jezrael Avatar answered Dec 07 '25 13:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!