简体   繁体   English

Python Pandas Dataframe - 创建新列

[英]Python Pandas Dataframe - Create new column using a conditional/applying a function based on another column

I'm new to Python so I'm still getting to grips with it.我是 Python 的新手,所以我仍在掌握它。

Essentially, in my Pandas Datframe I have a column 'Comments' and would like to create a new column called 'Readability' - where for non-null rows/values, I pass the 'Comments' value into textstat.flesch_reading_ease().本质上,在我的 Pandas Datframe 中,我有一个“评论”列,并希望创建一个名为“可读性”的新列 - 对于非空行/值,我将“评论”值传递给 textstat.flesch_reading_ease()。 But if 'Comments' are null/NaN - the value in 'Readability' would simply be 0.0.但是,如果“评论”为空/NaN - “可读性”中的值将只是 0.0。 . . The NaN values are part of my analysis - so I don't want to omit them in this case. NaN 值是我分析的一部分——所以我不想在这种情况下省略它们。

#Pseudo If null x -> 0.0, else textstat.flesch_reading_ease(x) #Pseudo If null x -> 0.0,否则 textstat.flesch_reading_ease(x)

See Image.见图片。

In terms of code, I have been building familiarity with pd.loc() - but I don't think it's viable in this case?在代码方面,我一直在熟悉 pd.loc() - 但我认为在这种情况下它不可行?

Alternatively, I have tried或者,我尝试过

repairs['Readibility'] = repairs['Comment'].apply(lambda x: 0.0 if x.isnull() else textstat.flesch_reading_ease(x))

This returns 'float' object has no attribute 'isnull'这将返回 'float' object 没有属性 'isnull'

Any ideas how to tweak my approach?任何想法如何调整我的方法? I would also appreciate the why/how behind solutions.我也很欣赏解决方案背后的原因/方式。 Also happy to see 2/3 step answers if it's easier to understand:)如果更容易理解,也很高兴看到 2/3 步的答案:)

Thanks!谢谢!

Example of 'Comments' column //i.stack.imgur.com/cZAWQ.png “评论”列示例 //i.stack.imgur.com/cZAWQ.png

Use:利用:

repairs['Readibility'] = repairs['Comment'].apply(lambda x: 0.0 if pd.isna(x) else textstat.flesch_reading_ease(x))

Another idea:另一个想法:

repairs['Readibility'] = 0
repairs['Readibility'] = repairs.loc['Comment'].notna(), "Comment"].apply(textstat.flesch_reading_ease)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Python / pandas基于来自另一个数据帧的列的内部总和来创建新的数据帧 - Trying to create a new dataframe based on internal sums of a column from another dataframe using Python/pandas 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? Pandas - 根据另一列的条件值创建新列 - Pandas - create new column based on conditional value of another column 基于变量值的新 python pandas 数据框列,使用函数 - new python pandas dataframe column based on value of variable, using function 根据另一列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in another column 如何根据 Python Pandas 中 DataFrame 中的另一列中的值创建具有隔间的新列? - How to create a new column with compartments based on values in another column in DataFrame in Python Pandas? Python:基于另一列和行的条件函数创建新列 - Python: Creating New Column Based on Conditional Function of Another Column and Row 如何在另一个数据帧 python pandas 中的多列上使用条件逻辑在数据帧中创建一列? - How can I create a column in a dataframe using conditional logic on multiple columns in another dataframe python pandas? 为pandas dataframe创建新列的条件要求 - conditional requirement to create new column for pandas dataframe 使用if语句针对另一列在pandas数据框中创建新列 - Create new column in pandas dataframe using if statement against another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM