ValueError: Columns must be same length as key: python replacing DF1's column with DF2

ValueError: Columns must be same length as key: python replacing DF1's column with DF2

我有两个数据框。 第一个数据帧,第二个数据帧。

我想根据 DF 2 替换国家/地区值。

Code Country Column C Column D
100 NULL foo mei
200 NULL bar tes
100 NULL foo1 mei1
200 NULL bar1 tes1
100 NULL foo2 mei2
200 NULL bar2 tes2
Code Country
100 Canada
200 USA

尝试过:

df1['Country'] = df2.replace(df1['Code'],df1['Country']

但这是抛出错误:列的长度必须与键相同

Code Country Column C Column D
100 Canada foo mei
200 USA bar tes
100 Canada foo1 mei1
200 USA bar1 tes1
100 Canada foo2 mei2
200 USA bar2 tes2

我不会使用替换方法,而是使用连接。

import pandas as pd

code = [100, 200, 100, 200]
df1 =  pd.DataFrame(code, columns=['code'])
df1["country"] = None

df1.head()

# return
code    country
100     None
200     None
100     None
200     None


code2 = [100, 200]
coutry2 = ["Canada", "USA"]
df2 = pd.DataFrame(list(zip(code2, coutry2)), columns=['code', 'country'])
df2.head()
# output
code    country
100     Canada
200     USA


resutls = pd.merge(df1, df2, on='code', how='left')
resutls.head()

# output
 code   country_x   country_y
100     None        Canada
200     None        USA
100     None        Canada
200     None        USA

results_clean = resutls[['code', 'country_y']]
results_clean.columns = ['code', 'country']
results_clean.head()

code    country
100     Canada
200     USA
100     Canada
200     USA