尝试使用两个pandas dataframes进行vlookup时麻烦

发布于 2025-01-26 14:19:08 字数 1026 浏览 5 评论 0原文

我读了很多关于此事的问题,但是这都没有解决我的问题。

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2025-02-02 14:19:08

如果要在df1的地址中更新值,则可以使用两种方法。

定位修改:

df1.update(df1['CodCourse']
           .map(df2.set_index('CodCourse')['Address'])
           .rename('Address'))

分配:

df1['Address'] = (df1['CodCourse']
                  .map(df2.set_index('CodCourse')['Address'])
                  .fillna(df1['Address'])
                 )

在两种情况下,这将替换DF2中具有新值的任何地址,同时保留没有新值的旧地址。

输出:

   CodCourse  Student     Address
0          1       10  Xth avenue
1          2       11  Yth avenue
2          2       12  Yth avenue
3          3       13  Zth avenue
4          3       14  Zth avenue
5          4       15  Nth avenue
6          4       16  Nth avenue

If you want to update the values in df1's address you can use two methods.

in place modification:

df1.update(df1['CodCourse']
           .map(df2.set_index('CodCourse')['Address'])
           .rename('Address'))

assignment:

df1['Address'] = (df1['CodCourse']
                  .map(df2.set_index('CodCourse')['Address'])
                  .fillna(df1['Address'])
                 )

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:

   CodCourse  Student     Address
0          1       10  Xth avenue
1          2       11  Yth avenue
2          2       12  Yth avenue
3          3       13  Zth avenue
4          3       14  Zth avenue
5          4       15  Nth avenue
6          4       16  Nth avenue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文