0

I am trying to merge two pandas df:

 Final_data
    sp_return  date  dj_return
0    0.000000  1927   0.306237
1    0.394938  1928   0.512283
2   -0.124227  1929  -0.176560
3   -0.235981  1930  -0.292411
4   -0.416280  1931  -0.478050
5   -0.054336  1932  -0.142564
6    0.454805  1933   0.654280
7   -0.073477  1934   0.025883
8    0.372718  1935   0.345200
9    0.260973  1936   0.230361
10  -0.402949  1937  -0.346893

And the following:

    date         president_name party
0   1921      Warren G. Harding     R
1   1922      Warren G. Harding     R
2   1923        Calvin Coolidge     R
3   1924        Calvin Coolidge     R
4   1925        Calvin Coolidge     R
5   1926        Calvin Coolidge     R
6   1927        Calvin Coolidge     R
7   1928        Calvin Coolidge     R
8   1929         Herbert Hoover     R
9   1930         Herbert Hoover     R
10  1931         Herbert Hoover     R
11  1932         Herbert Hoover     R

Using the following code:

final_data1 = presidents.merge(final_data,  on='date')

However, what I get at the end is an empty "final_data1" df. Do you have any suggestions why this might happen?

Please, advise me this issue.

1
  • 2
    Check the dtype of your dates. (See presidents.info() and final_data.info() for the dtypes). If one says "object" and the other "int", that would explain the problem. (If the values are strings, the dtype is reported as "object".) Commented Feb 12, 2018 at 14:08

2 Answers 2

4

Before merging make sure that datatypes are the same. Or add these lines:

presidents['date'] = presidents['date'].astype(int)
final_data['date'] = final_data['date'].astype(int)
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one - you get it ;)
0

My code:

df = pd.DataFrame({'Q4':[2906, 1508,738,206,154], 'i':1000})
df2 = pd.DataFrame({'Q4':[2906, 1508,738,206,1], 'j':2000})
df3 = df.merge(df2, on='Q4', how='outer')
df3.head()


      Q4         i       j
0   2906    1000.0  2000.0
1   1508    1000.0  2000.0
2   738     1000.0  2000.0
3   206     1000.0  2000.0
4   154     1000.0  NaN
5   1          NaN  2000.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.