gpt4 book ai didi

python - 如何在两个不同的列上合并两个 pandas 数据框,这些列的元素不按顺序排列?

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:49 26 4
gpt4 key购买 nike

<分区>

我有两个数据集,如下所示:

          name  Longitude   Latitude      continent
0 Aruba -69.982677 12.520880 North America
1 Afghanistan 66.004734 33.835231 Asia
2 Angola 17.537368 -12.293361 Africa
3 Anguilla -63.064989 18.223959 North America
4 Albania 20.049834 41.142450 Europe

另一个数据集是这样的:

          COUNTRY  GDP (BILLIONS) CODE
0 Afghanistan 21.71 AFG
1 Albania 13.40 ALB
2 Algeria 227.80 DZA
3 American Samoa 0.75 ASM
4 Andorra 4.80 AND

此处,nameCOUNTRY 列包含国家/地区名称,但顺序不同。

如何将第二个数据框合并到第一个数据框并将 CODE 列添加到第一个数据框。

要求的输出:

          name  Longitude   Latitude      continent   CODE
0 Aruba -69.982677 12.520880 North America NaN
1 Afghanistan 66.004734 33.835231 Asia AFG
2 Angola 17.537368 -12.293361 Africa NaN
3 Anguilla -63.064989 18.223959 North America NaN
4 Albania 20.049834 41.142450 Europe ALB

尝试:

import numpy as np
import pandas as pd

df = pd.DataFrame({'name' : ['Aruba', 'Afghanistan', 'Angola', 'Anguilla', 'Albania'],
'Longitude' : [-69.982677, 66.004734, 17.537368, -63.064989, 20.049834],
'Latitude' : [12.520880, 33.835231, '-12.293361', 18.223959, 41.142450],
'continent' : ['North America','Asia','Africa','North America','Europe'] })
print(df)

df2 = pd.DataFrame({'COUNTRY' : ['Afghanistan', 'Albania', 'Algeria', 'American Samoa', 'Andorra'],
'GDP (BILLIONS)' : [21.71, 13.40, 227.80, 0.75, 4.80],
'CODE' : ['AFG', 'ALB', 'DZA', 'ASM', 'AND']})
print(df2)


pd.merge(left=df, right=df2,left_on='name',right_on='COUNTRY')
# but this fails

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com