Python:使用条件和“包含”语句创建新列

时间:2019-02-01 17:33:26

标签: python-3.x pandas

我想创建一个带有条件的字符串新列。示例:

from pandas import DataFrame
GoT = {'Old_Group': ['Jon Snow', 'Sansa Stark','Arya Stark','Robb Stark','Theon Greyjoy' ]}
df = DataFrame(GoT,columns=['Old_Group'])

“ New_Group”应检查“ Old_Group”是否在任意位置包含字符串“ Stark”,并将其分配给例如“斯塔克家族”。如果条件“包含=‘斯塔克’”不适合,那么“new_group”应当被分配给例如“其他”

在SQL中,我会这样做:

Select Old_Group
,case when Old_Group like '%Stark%' then 'Stark Family' else 'other' end as New_Group
from df

谢谢

3 个答案:

答案 0 :(得分:1)

您可以结合使用np.where和str.contains来执行此操作。从本质上讲,您不能执行的操作是在相关列上使用np.where(在本例中为Old Group),然后检查字符串是否包含工作Stark。

function showClaimButton() {
var x = document.getElementById("claim");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

var v_modal = document.getElementById('modal_video');
var v_btn = document.getElementById('video-popup');
v_btn.onclick = function() {
v_modal.style.display = "block";
}
span.onclick = function() {
v_modal.style.display = "none";
setTimeout(showClaimButton(), 6000);
}
window.onclick = function(event) {
if(event.target == v_modal) {
v_modal.style.display = "none";
}
}

只需确保您的“新建组”列为字符串数据类型,并且已将numpy导入为包

答案 1 :(得分:1)

如果名称Stark以MacStark之类的名称出现,则此选项将其排除在外。也是不区分大小写的

df.assign(New_Column=df.replace({r'(?i)^((?!\bStark\b).)*$':'Other',r'(?i)\bStark\b':'Stark Family'},regex=True))
Out[319]: 
       Old_Group          New_Column
0       Jon Snow               Other
1    Sansa Stark  Sansa Stark Family
2     Arya Stark   Arya Stark Family
3     Robb Stark   Robb Stark Family
4  Theon Greyjoy               Other

答案 2 :(得分:0)

您需要:

    (SELECT model, price FROM PC) NATURAL JOIN (SELECT model FROM Product WHERE maker='B')

    UNION

    (SELECT model, price FROM Laptop) NATURAL JOIN (SELECT model FROM Product WHERE maker='B')

    UNION

    (SELECT model, price FROM Printer) NATURAL JOIN (SELECT model FROM Product WHERE maker='B');

输出

df['New_Group'] = df['Old_Group'].apply(lambda x : 'Stark Family' if 'Stark' in x else 'other')

print(df)