[英]Apply a user defined function to all rows of a specific column in python dataframe
I have hard times to apply a user defined function to a specific column in a python dataframe.我很难将用户定义的 function 应用于 python dataframe 中的特定列。 The dataframe is as fellow:
dataframe 是同胞:
Year state Narrative
----------------------------------
2015 WV a roof fall occurred at 10:05 am at 10+50 entry 6 in 8lms mmu 010, .. more text
2016 AL a rib rolled out striking him on his left foot resulting ...... more text
2017 CO a non-injury mountain bump occurred inby the 5n longwall. additional ... more text
I want to predict the type of ground failure based on "Narrative", such that a new column is added to the dataframe as shown below.我想根据“叙述”预测接地故障的类型,以便在 dataframe 中添加一个新列,如下所示。 I predict the ground fall through looking for some keywords in the "narrative", for example: if the "narrative" includes any of the following words
['roof fall', 'roof broke', 'rock fell from the top']
, the ground fall prediction should be "roof fall".我通过在“narrative”中查找一些关键字来预测地面塌陷,例如:如果“narrative”包含以下任何单词
['roof fall', 'roof broke', 'rock fell from the top']
,地面坠落预测应该是“屋顶坠落”。
This is the user defined function that I generated, but it is not working.这是我生成的用户定义的 function,但它不起作用。
def predict_groundFall(narrative):
fall_dict = {'roof fall': ['Roof fall', 'roof broke', 'rock fell from the top'],
'rib fall': ['rib fall ', 'rib rolled', 'rib dislodged'],
'outburst': ['outburst', 'bounce', 'rockburst']}
for key, values in fall_dict.iteritems():
if values in narrative:
return key
break
df['predicted_failure'] = df.apply( lambda row: predict_groundFall( row['Narrative']), axis=1)
this is what I want to achieve: adding a new column to predict the failure from the narrative.这就是我想要实现的:添加一个新列来预测叙述中的失败。
Year state Narrative predicted_failure
------------------------------------------------------------- ---------------------
2015 WV a roof fall occurred ....... more text.... roof fall
2016 AL a rib rolled out striking ......more text .... rib fall
2017 CO a non-injury mountain ....... more text.... outburst
I am new to Python, so I hope you help me fix the code to make it work.我是 Python 的新手,所以希望您能帮我修复代码以使其正常工作。 A better method to achieve my goal is highly appreciated.
高度赞赏实现我目标的更好方法。 thank you in advance,
先感谢您,
Your function isn't working as expected.您的 function 未按预期工作。 You want to try the following:
您想尝试以下方法:
def predict_groundFall(narrative):
fall_dict = {'roof fall': ['Roof fall', 'roof broke', 'rock fell from the top'],
'rib fall': ['rib fall ', 'rib rolled', 'rib dislodged'],
'outburst': ['outburst', 'bounce', 'rockburst']}
for key in fall_dict:
if any(v.lower() in narrative.lower() for v in fall_dict[key]):
return key
Then change your column assignment to the following:然后将您的列分配更改为以下内容:
df['predicted_failure'] = df["Narrative"].apply(lambda x: predict_groundFall(x))
I think the problem is in your apply function.我认为问题出在您的申请 function 中。
change this line df['predicted_failure'] = df.apply( lambda row: predict_groundFall( row['Narrative']), axis=1)
更改此行
df['predicted_failure'] = df.apply( lambda row: predict_groundFall( row['Narrative']), axis=1)
to至
df['predicted_failure'] = df.Narrative.apply(predict_groundFall)
this will send each value of Narrative
to your custom function and then populate the new column with the return from that function这会将
Narrative
的每个值发送到您的自定义 function,然后使用来自该 function 的返回填充新列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.