简体   繁体   English

将用户定义的 function 应用于 python dataframe 中特定列的所有行

[英]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.

相关问题 向量化熊猫数据框将函数应用于python中的用户定义函数 - Vectorising pandas dataframe apply function for user defined function in python "如何将用户定义的函数应用于熊猫数据框中的列?" - How to apply a user-defined function to a column in pandas dataframe? 将用户定义的函数应用于DataFrame失败,Python 3.6 - Apply user defined function to DataFrame fails, Python 3.6 如何将数据帧行应用到定义的函数中 - how to apply dataframe rows into a defined function 用户在 DataFrame 列上定义的 Function - User Defined Function on a column of DataFrame 如何将 dataframe 中的所有列传递到不同的用户定义的 function - how to pass all the column from dataframe into different user defined function C 应用用户定义的 function 到 pandas dataframe 特定列并将新列添加到 Z6A8064B53DF47945555707 - apply user defined function to pandas dataframe specific columns and add new columns to dataframe 将函数应用于DataFrame中特定数量的行 - Apply function to a specific number of rows in a DataFrame 如何使用 apply(基于用户定义的函数)向数据框添加新行和多行? - How to add new and multiple rows to a dataframe using apply (based on a user-defined function)? 将 function 应用于 pandas dataframe (lambda) 中的所有行 - Apply function to all rows in pandas dataframe (lambda)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM