Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Merge returns NaN

Tags:

I have issues with the merging of two large Dataframes since the merge returns NaN values though there are fitting values. The two dfs are shaped like:

df1

Motor 2232 1524 2230 2230 2224 1516 1724 2224 1524 1624 1724 2224 2224 1524 1524 1516 1524 2224 1624 1724 1724 2224 2224 

df2

Motor   Output Torque (mNm) 0615    0,17 1219    0,72 1516    0,59 1624    2 2230    4,7 2233    5,9 0816    0,7 1016    0,92 1024    1,6 1224    1,7 1319    1,4 1331    3,8 1516    0,97 1524    2,9 1717    2,2 1724    4,5 2224    6,8 2232    10 1336    3,6 1727    4,9 1741    8,8 2237    12 2642    26 

I use the code:

MergeDat=MergeDat.merge(Motor,how="left") print(MergeDat) 

where MergeDat= df1 and Motor= df2

As result it returns:

  Motor  Output Torque (mNm) 0      2232                  NaN 1      1524                  NaN 2      2230                  NaN 3      2230                  NaN 4      2224                  NaN 5      1516                  NaN 6      1724                  NaN 7      2224                  NaN 8      1524                  NaN 9      1624                  NaN 10     1724                  NaN 11     2224                  NaN 12     2224                  NaN 13     1524                  NaN 14     1524                  NaN 15     1516                  NaN 16     1524                  NaN 17     2224                  NaN 18     1624                  NaN 19     1724                  NaN 20     1724                  NaN 21     2224                  NaN 22     2224                  NaN 23     1524                  NaN 24     1724                  NaN 25     1841                  NaN 26     2224                  NaN 

I have no idea why the Output Torque column is not merged...

Appreciate any help!

like image 807
2Obe Avatar asked Oct 15 '17 11:10

2Obe


People also ask

Does Pandas merge NaN?

To merge two Pandas DataFrame with common column, use the merge() function and set the ON parameter as the column name. To set NaN for unmatched values, use the “how” parameter and set it left or right. That would mean, merging left or right.

What does merge do in Pandas?

The merge() method updates the content of two DataFrame by merging them together, using the specified method(s). Use the parameters to control which values to keep and which to replace.

What is left on and right on in Pandas merge?

left_on − Columns from the left DataFrame to use as keys. Can either be column names or arrays with length equal to the length of the DataFrame. right_on − Columns from the right DataFrame to use as keys. Can either be column names or arrays with length equal to the length of the DataFrame.

How do I merge two DataFrames in Pandas?

The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause. 'left', 'right' and 'inner' joins are all possible.


1 Answers

You need same dtype of joined columns:

#convert first or second to str or int MergeDat['Motor'] = MergeDat['Motor'].astype(str) #Motor['Motor'] = Motor['Motor'].astype(str)  #MergeDat['Motor'] = MergeDat['Motor'].astype(int) Motor['Motor'] = Motor['Motor'].astype(int) 

#convert first or second to str or int #MergeDat['Motor'] = MergeDat['Motor'].astype(str) Motor['Motor'] = Motor['Motor'].astype(str)  MergeDat['Motor'] = MergeDat['Motor'].astype(int) #Motor['Motor'] = Motor['Motor'].astype(int)   MergeDat=MergeDat.merge(Motor,how="left") 
like image 131
jezrael Avatar answered Oct 20 '22 00:10

jezrael