如何在Pandas中基于搜索子字符串创建规则集以将值分配给特定列? [英] How can i create a ruleset to assign values to specific columns, based on searching substrings, in Pandas?
问题描述
我是Python(和Pandas库)的新手,需要在其中重新创建一些SQL代码。
I am a complete newbie to Python (and Pandas library) and need to recreate some SQL code in it.
我的任务非常简单,我有几列,我需要搜索它们的特定字符串,如果它们存在,则将值放在类别列中。
My task is quite simple of the face of it, I have a few columns, and i need to search them for specific strings, and if they exist then a value is placed in category columns.
例如
import pandas as pd
phone_ds= [('IPHONE_3UK_CONTRACT', 968), ('IPHONE_O2_SIMONLY', 155), ('ANDROID_3UK_PAYG', 77), , ('ANDROID_VODAF_CONTRACT', 973)]
a = pd.DataFrame(data=phone_ds, columns=['Names', 'qty'])
def f(a):
if a['Names'].str.contains('3UK'):
company = 'Three'
if a['Names'].str.contains('iPhone'):
OS = 'iOS'
.
.
.
etc
是否有比列出if语句更好(更有效)的方法?
Is there a better (more efficient) way than listing if statements?
我如何将结果添加到新列中?
How would i go about adding the results into new columns?
谢谢
推荐答案
找到了一种方法,但不确定它是否最有效。如果它遵循我上面发布的相同逻辑,如果它将创建一个带有规则的函数。规则将在预定义的搜索词列表中查找,然后为规则创建一个新列。
Found a way to do this, but not sure if it is most efficient. If would follow the same logic as i posted above, if that it would create a function with rules. The rules would look in a list of pre-defined search words, then create a new column for the rules.
每列都需要自己的功能,所以要添加3电话,承运人,合同类型的列,我创建了3个功能。
Each column would require its own function, so to add 3 columns for Phone, Carrier, Contract Type, i created 3 functions.
如下所示:
android_phones = ['samsung','xperia','google']
iphone= ['iphone','apple']
def OS_rules(raw_Df):
val=''
if any(word in raw_Df['Names'].lower() for word in android_phones):
val='android'
elif any(word in raw_Df['Names'].lower() for word in iphone):
val='iPhone'
else: val = 'Handset'
return val
df.loc[:,'OS_Type']=df.apply(OS_rules,axis=1)
这篇关于如何在Pandas中基于搜索子字符串创建规则集以将值分配给特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!