简体   繁体   中英

Combining data frames in python

I have some currencies in json files, I created. For example

[{"AUD":"2.8681"},{"AUD":"2.8667"},...], [{"BRL":"1.1607"}, {"BRL":"1.1645"},...].

import json
 import pandas as pd
 with open('AUD.json') as f:
    AUD = json.load(f)
 with open("BRL.json") as f:
    BRL=json.load(f)
 df = pd.DataFrame.from_dict(AUD)
 df2= pd.DataFrame.from_dict(BRL)
 print(df+df2)

Output is

 AUD  BRL
0    NaN  NaN
1    NaN  NaN
2    NaN  NaN
       ...

What I want is

 AUD  BRL
0    2.8681  1.1607
1    2.8667  1.1645
2    2.8679  1.1634
     ...

Thank you in advance for your solutions ;)

Edit: print df.head() AUD 0 2.8681 1 2.8667 2 2.8738 3 2.8589 4 2.8598 df2.head() works also well print(pd.concat([df,df2]) Output

AUD BRL 0 2.8681 NaN 1 2.8667 NaN ... 124 NaN 1.0299 125 NaN 1.033

您犯了一个小错误,您要做的是

pd.concat([df, df2], axis=1)

You need:

pd.concat([df,df2], axis=1)

Output will be of form:

       AUD  BRL
0   2.8681  2.8681
1   2.8667  2.8667
2   2.8738  2.8738
3   2.8589  2.8589
4   2.8598  2.8598

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM