尝试使用两个pandas dataframes进行vlookup时麻烦
我读了很多关于此事的问题,但是这都没有解决我的问题。
我有2个数据范围,一个包含一个国家 /地区的所有毕业级别的列表,每个毕业生(每行)都有有关学生本身以及课程代码的信息。
在另一个数据范围内,我有一个唯一的课程代码列表,其中包含分配给课程代码的大学地址。
df1
CodCourse|Student|Address
1 10 outdated address
2 11 outdated address
2 12 outdated address
3 13 outdated address
3 14 outdated address
4 15 outdated address
4 16 outdated address
df2:
CodCourse Address
1 Xth avenue
2 Yth avenue
3 Zth avenue
4 Nth avenue
Expected result:
df1
CodCourse|Student|Address
1 10 Xth avenue
2 11 Yth Street
2 12 Yth Street
3 13 Zth Street
3 14 Zth Street
4 15 Nth Street
4 16 Nth Street
我想用数据框2的地址列更新数据框1地址列。
我这样做了,但是它不起作用。我已经尝试使用加入并使用词典,但我所拥有的只是失败。
df1=df1.merge(df2[['CodCourse','Address']], on='CodCourse', how='left')
拜托,谁能帮我吗?
谢谢! 爱德华多。
I've read a lot of questions regarding this matter, but none of it solved my problem.
I have 2 dataframes, one containing a list of all students of graduation level in a country, each one (each row) with informations about the student itself, as well as the course code.
On another dataframe, i have a list of unique course codes containing the address of the university that is assigned to the course code.
df1
CodCourse|Student|Address
1 10 outdated address
2 11 outdated address
2 12 outdated address
3 13 outdated address
3 14 outdated address
4 15 outdated address
4 16 outdated address
df2:
CodCourse Address
1 Xth avenue
2 Yth avenue
3 Zth avenue
4 Nth avenue
Expected result:
df1
CodCourse|Student|Address
1 10 Xth avenue
2 11 Yth Street
2 12 Yth Street
3 13 Zth Street
3 14 Zth Street
4 15 Nth Street
4 16 Nth Street
I want to update the dataframe 1 address column with the address column of the dataframe 2.
I'm doing like this, but it's not working. I've tried with join and using a dictionary, but all I have is a failure.
df1=df1.merge(df2[['CodCourse','Address']], on='CodCourse', how='left')
Please, can anyone help me?
Thanks!
Eduardo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在
df1
的地址中更新值,则可以使用两种方法。定位修改:
分配:
在两种情况下,这将替换DF2中具有新值的任何地址,同时保留没有新值的旧地址。
输出:
If you want to update the values in
df1
's address you can use two methods.in place modification:
assignment:
In both case this will replace any Address that has a new value in df2 while keeping old addresses that don't have a new value.
output: