I have two pandas dataframes I want to combine based on the value of a common column in the dataframe. However in one of the dataframes the values in the column are not unique:
df1 = pd.DataFrame(
{'SimId:': [1, 1, 1, 2, 2],
'RunId': [1, 2, 3, 1, 2],
'Velocity': [5, 6, 7, 8, 9]})
df2 = pd.DataFrame(
{'SimId': [1, 2],
'weather': ['sun', 'snow']})
As a result I would like to get a dataframe like this:
df3 = pd.DataFrame(
{'SimId:': [1, 1, 1, 2, 2],
'RunId': [1, 2, 3, 1, 2],
'Velocity': [5, 6, 7, 8, 9],
'weather': ['sun', 'sun', 'sun', 'snow', 'snow']})
When trying to merge like this:
df3 = pd.merge(df1, df2, on='SimId', how='right')
I get a "KeyError".
Can anyone help me with what is the most pythonic way to solve this?
Your code works:
df3 = pd.merge(df1, df2, on='SimId', how='right')
You just need to fix a typo in df1: not 'SimId:', but 'SimId'.
your code works as Andrey said just fix a typo in df1
df1 = pd.DataFrame(
{'SimId': [1, 1, 1, 2, 2],
'RunId': [1, 2, 3, 1, 2],
'Velocity': [5, 6, 7, 8, 9]})
df2 = pd.DataFrame(
{'SimId': [1, 2],
'weather': ['sun', 'snow']})
df3 = pd.merge(df1, df2, on='SimId', how='right')
print (df3)
RunId SimId Velocity weather
# 0 1 1 5 sun
# 1 2 1 6 sun
# 2 3 1 7 sun
# 3 1 2 8 snow
# 4 2 2 9 snow
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.