How to add values from list as a row in DataFrame if values from list do not exist in DF with defined values in other columns in DF in Python Pandas?

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

How to add values from list as a row in DataFrame if values from list do not exist in DF with defined values in other columns in DF in Python Pandas?

问题

我有一个类似下面的Python中的Pandas DataFrame:

示例数据:

COL1  | COL2 | COL3
------|------|-------
var1  | xxx  | 20
var2  | xxx  | 10
var3  | yyy  | 10

我有一个类似如下的列表:list_1 = ["var1", "var5"]

要求:

我需要:

  1. 仅在DataFrame的"COL1"中的值不在list_1中存在时,将list_1中的值添加为行。
  2. 在"COL1"中添加的每个值中,"COL2"中需要有"yyy","COL3"中需要有"10"。

期望的输出:

因此,根据我的示例DataFrame和list_1,我需要类似下面的结果:

COL1  | COL2 | COL3
------|------|-------
var1  | xxx  | 20
var2  | xxx  | 10
var3  | yyy  | 10
var5  | yyy  | 10

如何在Python Pandas中实现这个目标?

英文:

I have Pandas DataFrame in Python like below:

Example data:

COL1  | COL2 | COL3
------|------|-------
var1  | xxx  | 20
var2  | xxx  | 10
var3  | yyy  | 10

And I have list like the follow: list_1 = ["var1", "var5"]

Requirements:

And I need to

  1. add to "COL1" in DataFrame values from list_1 as row only if values from list_1 do not exist in "COL1" in DataFrame
  2. In each added in "COL1" values from list I need to have values "yyy" in "COL2" and "10" in "COL3"

Desire output:

So, as a result I need something like below based on my example DataFrame and list_1:

COL1  | COL2 | COL3
------|------|-------
var1  | xxx  | 20
var2  | xxx  | 10
var3  | yyy  | 10
var5  | yyy  | 10

How can I do that in Python Pandas ?

答案1

得分: 3

您可以使用集合操作与 concat 结合使用:

out = pd.concat([df, pd.DataFrame({'COL1': list(set(list_1).difference(df['COL1']))}
                                 ).assign(COL2='yyy', COL3=10)])

输出:

   COL1 COL2  COL3
0  var1  xxx    20
1  var2  xxx    10
2  var3  yyy    10
0  var5  yyy    10
英文:

You can use set operations combined with concat:

out = pd.concat([df, pd.DataFrame({'COL1': list(set(list_1).difference(df['COL1']))}
                                 ).assign(COL2='yyy', COL3=10)])

Output:

   COL1 COL2  COL3
0  var1  xxx    20
1  var2  xxx    10
2  var3  yyy    10
0  var5  yyy    10

答案2

得分: 2

一种方法是遍历列表并调用 append,例如:

for elem in list_1:
    if elem not in df["COL1"].values:
        df = df.append({"COL1": elem, "COL2": "yyy", "COL3": 10}, ignore_index=True)
英文:

One way would be to iterate through the list and call append, like:

for elem in list_1:
    if elem not in df["COL1"].values:
        df = df.append({"COL1": elem, "COL2": "yyy", "COL3": 10}, ignore_index=True)

huangapple
  • 本文由 发表于 2023年3月7日 16:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659832.html
匿名

发表评论

匿名网友

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

确定