选择Pandas中的行并迭代它们,以根据另一行中的值更改列中的值。

huangapple go评论62阅读模式
英文:

Select rows in Pandas and iterate thru them to change value in column based in value in another row

问题

我有一个数据框,其中每一行代表一个对象。
我想选择具有特定列中特定值的所有行。然后,我想遍历所选行,并根据行中另一列中的值添加一个值到另一列中。

我会使用loc来选择行,但然后我不知道如何迭代它们以检查值并更改它们。

这可能是一个广泛的问题,但我是Pandas的新手。我搜索了类似的问题,但没有找到我要找的答案。

示例数据

id name city address is customer
1 John Berlin bla
2 Jake Paris bla
3 Jim Berlin Club

我想只选择城市为“Berlin”的行。
然后在客户列表中查找名字:

list_of_customers = ["John","Jake"]

如果找到名字,就在“is customer”列中添加“True”。

id name city address is customer
1 John Berlin bla True
2 Jake Paris bla
3 Jim Berlin Club

我更喜欢更改原始的数据框而不是创建一个新的。

英文:

I have a data frame where every row represents an object.
I want to select all rows that have a specific value in a column. Then I want to iterate thru the selected rows and and add a value in a column based on a value in another column on the row.

I would use loc to select the rows, but then I have no idea how to iterate over them to check value and change them.

This might be a broad question, but I am new to Pandas. I did search for similar question, but did not find what I was searching for.

Example Data

id name city address is customer
1 John Berlin bla
2 Jake Paris bla
3 Jim Berlin Club

I want to select only rows with "Berlin" as city.
Then lookup the name in the list of customers:

list_of_customers = ["John","Jake"]

And if the name is found ad a "True" in the column "is customer"

id name city address is customer
1 John Berlin bla True
2 Jake Paris bla
3 Jim Berlin Club

I prefer to change the original DF not create a new one.

答案1

得分: 1

现在你澄清了你的问题,我可以帮助:

import numpy as np
list_of_customers = ["john","jake"]
df['is customer'] = np.where(df['name'].str.lower().isin(list_of_customers), True, False)
英文:

Now that you clarified your question I can help:

import numpy as np
list_of_customers = ["john","jake"]
df ['is customer'] = np.where(df['name'].str.lower().isin(list_of_customers), True, False)

huangapple
  • 本文由 发表于 2023年7月24日 17:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定