'ValueError: Columns must be same length as key: python replacing DF1's column with DF2
I have two dataframes. One dataframe one first, dataframe two second.
I want to replace country value based on 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 |
Tried:
df1['Country'] = df2.replace(df1['Code'],df1['Country']
But this is throwing error: Columns must be same length as key
| 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 |
Solution 1:[1]
I would not use the replace method but a join.
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Enrique Benito Casado |
