如何在python中为自定义函数使用apply函数
我有这个功能如何在python中为自定义函数使用apply函数,python,pandas,Python,Pandas,我有这个功能 def cleanup_model(model, clean_model_list): model = model.lower() clean_model_list = [x.lower() for x in clean_model_list] for x in clean_model_list: if x in model: return x return model 我有这个数据框 d = {'lapto
def cleanup_model(model, clean_model_list):
model = model.lower()
clean_model_list = [x.lower() for x in clean_model_list]
for x in clean_model_list:
if x in model:
return x
return model
我有这个数据框
d = {'laptops' : ['apple macbook pro 16','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)
我有这个清单
l = ['apple macbook', 'hp laptop']
我想使用pandas中的apply
函数来获取此信息
d = {'laptops' : ['apple macbook','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)
我有点困惑如何将上述函数应用于整个数据帧。感谢您的建议。dataframe.apply(f)
用于获取一个参数并返回一个输出的函数
目前,您的函数接受2个参数,因此.apply(cleanup\u model)
将无法工作
如果您事先知道clean_model_list的值,我建议您这样做:(假设l=['apple macbook','hp laptop']
是您的clean_model_list
def cleanup_model(model,clean_model_list=['apple macbook','hp膝上型电脑]):
model=model.lower()
clean_model_list=[x.lower()表示clean_model_list中的x]
对于clean_model_列表中的x:
如果模型中有x:
返回x
回归模型
a、 应用(清理模型)
或者,你也可以去
clean_model_list=['apple macbook','hp膝上型电脑']
def清理_模型(模型):
model=model.lower()
clean_model_list=[x.lower()表示clean_model_list中的x]
对于clean_model_列表中的x:
如果模型中有x:
返回x
回归模型
a、 应用(清理模型)
dataframe.apply(f)
用于获取一个参数并返回一个输出的函数
目前,您的函数接受2个参数,因此.apply(cleanup\u model)
将无法工作
如果您事先知道clean_model_list的值,我建议您这样做:(假设l=['apple macbook','hp laptop']
是您的clean_model_list
def cleanup_model(model,clean_model_list=['apple macbook','hp膝上型电脑]):
model=model.lower()
clean_model_list=[x.lower()表示clean_model_list中的x]
对于clean_model_列表中的x:
如果模型中有x:
返回x
回归模型
a、 应用(清理模型)
或者,你也可以去
clean_model_list=['apple macbook','hp膝上型电脑']
def清理_模型(模型):
model=model.lower()
clean_model_list=[x.lower()表示clean_model_list中的x]
对于clean_model_列表中的x:
如果模型中有x:
返回x
回归模型
a、 应用(清理模型)
您可以使用args
参数或通过传递关键字参数将参数传递给apply
(在本例中使用)
def cleanup_model(model, clean_model_list):
model = model.lower()
clean_model_list = [x.lower() for x in clean_model_list]
for x in clean_model_list:
if x in model:
return x
return model
d = {'laptops' : ['apple macbook','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)
l = ['apple macbook', 'hp laptop']
a.laptops.apply(cleanup_model, args=(l,))
这会给你
0 apple macbook
1 apple macbook
2 hp laptop
Name: laptops, dtype: object
您也可以像这样使用关键字参数
a.laptops.apply(cleanup_model, clean_model_list=l)
您可以使用
args
参数或通过传递关键字参数将参数传递给apply
(在本例中使用)
def cleanup_model(model, clean_model_list):
model = model.lower()
clean_model_list = [x.lower() for x in clean_model_list]
for x in clean_model_list:
if x in model:
return x
return model
d = {'laptops' : ['apple macbook','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)
l = ['apple macbook', 'hp laptop']
a.laptops.apply(cleanup_model, args=(l,))
这会给你
0 apple macbook
1 apple macbook
2 hp laptop
Name: laptops, dtype: object
您也可以像这样使用关键字参数
a.laptops.apply(cleanup_model, clean_model_list=l)
如何调用
cleanup\u model
?如何调用cleanup\u model
?