Python If、elif和else在数据框中创建新列
正在尝试在数据帧调用“方法”中创建新列。所附图片中的当前数据帧: 我试图使用if/elif/else以及regex来创建新列,但是当我运行这段代码时,我只得到来自else语句的值。为什么这不起作用?我该如何修复它Python If、elif和else在数据框中创建新列,python,regex,pandas,if-statement,series,Python,Regex,Pandas,If Statement,Series,正在尝试在数据帧调用“方法”中创建新列。所附图片中的当前数据帧: 我试图使用if/elif/else以及regex来创建新列,但是当我运行这段代码时,我只得到来自else语句的值。为什么这不起作用?我该如何修复它 if 'posted' in df2.Full.astype(str) and '/ Outbound' in df2.TPrev.astype(str): df2['Method']='Classifieds Homepage Button' elif 'ad posted'
if 'posted' in df2.Full.astype(str) and '/ Outbound' in df2.TPrev.astype(str):
df2['Method']='Classifieds Homepage Button'
elif 'ad posted' in df2.Full.astype(str) and 'thanks' in df2.TPrev.astype(str):
df2['Method']='Header after Post'
elif 'ad posted' in df2.Full.astype(str) and '/myaccount/listing-classified Outbound' in df2.TPrev.astype(str):
df2['Method']='My Listings Button'
elif 'ad posted' in df2.Full.astype(str) and '/s/' in df2.TPrev.astype(str):
df2['Method']='SRP'
elif 'ad posted' in df2.Full.astype(str) and '/myaccount/listing-classified nan' in df2.TPrev.astype(str):
df2['Method']='My Listings Button'
elif 'ad posted' in df2.Full.astype(str) and '/sell nan nan' in df2.TPrev and '/myaccount/listing-classified nan nan' in df2.Prev.astype(str):
df2['Method']='My Listings Header'
elif 'ad posted' in df2.Full.astype(str) and '/listing/' in df2.TPrev.astype(str):
df2['Method']='Detail Page Header'
elif 'ad posted' in df2.Full.astype(str) and '/search/' in df2.TPrev.astype(str):
df2['Method']='SRP'
else:
df2['Method']='Ignore'
正如评论中的人所建议的,问题是当你给一列指定一个值时,你只需重写所有列,使其具有与你指定的值相同的值。 您要做的是:
df2.astype(str)
conditions = [
('posted' in df2.Full) & ('/ Outbound' in df2.TPrev),
('ad posted' in df2.Full) & ('thanks' in df2.TPrev),
('ad posted' in df2.Full) & ('/myaccount/listing-classified Outbound' in df2.TPrev)]
choices = ['"Classified Homepage Button"', 'Header after Post', 'My Listings Button']
df2['Method'] = np.select(conditions, choices, default='Ignore')
您的逻辑语句对整个数据帧求值为一个真值,因此您只能将整个列设置为其中一个值。相反,您应该使用
np。选择类似于中的
在每行应用条件逻辑。您可能还希望将语句切换为df2.Full.astype(str).str.contains('ad posted')
,因为这些语句返回布尔序列。正如上面的注释所述,每次执行都会覆盖df2['Method']
。除了np.选择
,您可以先创建一个空的df2['Method']
列,然后使用循环填写您的条件。您没有阅读熊猫文档吗?这是否回答了您的问题?
conditions = [
('posted' in df2.Full) & ('/ Outbound' in df2.TPrev),
('ad posted' in df2.Full) & ('thanks' in df2.TPrev),
('ad posted' in df2.Full) & ('/myaccount/listing-classified Outbound' in df2.TPrev)]
choices = ['"Classified Homepage Button"', 'Header after Post', 'My Listings Button']
df2['Method'] = np.select(conditions, choices, default='Ignore')