问题
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