Gunjan Dewan Gunjan Dewan - 7 months ago 203
Python Question

Pandas : merging 2 dataframes

New to pandas so please bare with me.
I am trying to merge the following 2 dataframes.

df.head():

x y z w
0 0.0056 11 824 51
1 0.0056 138 546 16
2 0.0056 328 1264 40
3 0.0056 1212 553 91
4 0.0056 1839 388 48


df1.head():

x y z
0 5539.0567 12243 27
1 5873.2923 14474 1540
2 3975.9776 11353 699
3 1508.5975 8250 628
4 66.7913 11812 538


using the following command:

df1 = df1.merge(df, how='left',left_on=(['x','y','z']),right_index=True)


and the following error crops up:


ValueError: len(left_on) must equal the number of levels in the index of "right"


In total df has 11458060 rows and df1 has 2528243 rows

I dont really know what this means. Can anyone tell me what I might be doing wrong?

Answer

I think you need merge on columns x, y and z:

print df1.merge(df, how='left', on=['x','y','z'])

Docs.

Parameter on in merge:

on: Columns (names) to join on. Must be found in both the left and right DataFrame objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames will be inferred to be the join keys

Comments