pandas append on columns with different names

為{幸葍}努か 提交于 2021-02-07 21:00:37

问题


How to append 2 different dataframes with different column names

a = pd.DataFrame({
    "id": [0,1,2,3],
    "countryid": [22,36,21,64],
    "famousfruit": ["banana", "apple", "mango", "orange"],
    "famousanimal": ["monkey", "elephant", "monkey", "horse"],
    "waterlvl": [23, 43, 41, 87]
}).set_index("id")

>> a

b = pd.DataFrame({
    "id": [0,1,2,3],
    "cid": [25,27,98,67],
    "FAM_FRUIT": ["grapes", "pineapple", "avacado", "orange"],
    "FAM_ANI": ["giraffe", "dog", "cat", "horse"],
}).set_index("id")

>>b

How to append the rows on b on the respective columns(whose names are different compared to a) and produce a result like below c


回答1:


Easiest way I can think of to do this is to simply rename the columns in b to match those in a, then use the Pandas concat function. Also best to reset index if using this method

b.rename(columns={'FAM_FRUIT': 'famousfruit',
                 'FAM_ANI': 'famousanimal',
                 'cid': 'countryid'}, inplace=True)
a = pd.concat([a, b])
a.reset_index(inplace=True, drop=True)



回答2:


Outer join via pd.merge is one way. Since this an outer join, on parameter need not be specified as pandas will use common columns.

b = b.rename(columns={'FAM_FRUIT': 'famousfruit',
                      'FAM_ANI': 'famousanimal',
                      'cid': 'countryid'})

a.merge(b, how='outer')

#    countryid famousanimal famousfruit  waterlvl
# 0         22       monkey      banana      23.0
# 1         36     elephant       apple      43.0
# 2         21       monkey       mango      41.0
# 3         64        horse      orange      87.0
# 4         25      giraffe      grapes       NaN
# 5         27          dog   pineapple       NaN
# 6         98          cat     avacado       NaN
# 7         67        horse      orange       NaN


来源:https://stackoverflow.com/questions/48792037/pandas-append-on-columns-with-different-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!