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?

Question:

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 ?

Asked By: dingaro

||

Answers:

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
Answered By: mozway

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)
Answered By: P.Jo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.