英文:
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"]
要求:
我需要:
- 仅在DataFrame的"COL1"中的值不在
list_1
中存在时,将list_1
中的值添加为行。 - 在"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
- add to "COL1" in DataFrame values from list_1 as row only if values from
list_1
do not exist in "COL1" in DataFrame - 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论