遍历数据框中的行并根据其他列更改列的值 [英] Iterate through rows in a dataframe and change value of a column based on other column

查看:90
本文介绍了遍历数据框中的行并根据其他列更改列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 df 的数据框,如下所示:

Assuming I have a dataframe called df which looks like the one shown below:

Id      Place        
1        NY        
2       Berlin          
3       Paris        
4       Paris         
5       Berlin       

还有一个字典,其ID为键,其位置为值,如下所示:

And a dictionary, which has IDs as keys and places as values as shown below:

id_to_place = { 1 : "Berlin", 2: "Berlin", 3: "NY"}

我想遍历数据帧的每一行,并查看ID是否包含在 id_to_place 词典中。如果是这样,那么我想用字典值替换该行的 Place 列。例如,在runninh之后,我希望输出的代码是:

I want to iterate through every row of the dataframe and see if the ID is contained in the id_to_place dictionary. If so, then I wanna replace the column Place of that row with the dictionary value. For instance after runninh the code I want the output to be:

Id      Place        
1       Berlin       
2       Berlin          
3       NY        
4       Paris         
5       Berlin       

到目前为止我尝试了以下代码:

So far I have tried this code:

id_to_place = { 1 : "Berlin", 2: "Berlin", 3: "NY"}

for index,row in df.iterrows():
    id = row['id']
    place = row['place']
    for item in id_to_place:
        if item == str(id):
          df.loc[df.id =id,'place'] = id_to_place[item]

print(df)

但是当我运行代码时,数据帧保持不变。有谁知道为什么会这样吗?感谢您的帮助!

But when I run the code the dataframe stays unchangable. Does anyone have an idea as to why this happens? I appreciate any help!

推荐答案

您当前的方法不起作用,因为您词典中的项目是整数并且您正在检查它们与始终返回False的str(id)相对。如果您删除str并仅检查id项,则它起作用。

Your current method isn't working because your items in your dictionaries are integers and you're checking them against str(id) which always returns False. If you remove the str and just check item against id then it works.

id_to_place = { 1 : "Berlin", 2: "Berlin", 3: "NY"}

for index,row in df.iterrows():
    id = row['id']
    place = row['place']
    for item in id_to_place:
        if item == id: # this line changed
          df.loc[df.id =id,'place'] = id_to_place[item]

print(df)

这篇关于遍历数据框中的行并根据其他列更改列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆