简体   繁体   English

尝试通过使用 if 语句过滤另一列在 pandas dataframe 中创建一个新列

[英]Trying to make a new column in pandas dataframe by filtering another column using a if statement

Trying to make a column named loan_status_is_great on my pandas dataframe.试图在我的 pandas dataframe 上创建一个名为 loan_status_is_great 的列。 It should contain the integer 1 if loan_status is "Current" or "Fully Paid."如果loan_status 是“当前”或“全额支付”,它应该包含integer 1。 Else it should contain the integer 0.否则它应该包含 integer 0。

I'm using https://resources.lendingclub.com/LoanStats_2018Q4.csv.zip as my dataset.我使用https://resources.lendingclub.com/LoanStats_2018Q4.csv.zip作为我的数据集。

My problem code is:我的问题代码是:

def loan_great():
   if (df['loan_status']).any == 'Current' or (df['loan_status']).any == 'Fully Paid':
     return 1
   else:
     return 0

df['loan_status_is_great']=df['loan_status'].apply(loan_great())

TypeError Traceback (most recent call last) in () ----> 1 df['loan_status_is_great']=df['loan_status'].apply(loan_great()) () 中的 TypeError Traceback (最近一次调用最后一次) ----> 1 df['loan_status_is_great']=df['loan_status'].apply(loan_great())

/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds) 4043 else: 4044 values = self.astype(object).values -> 4045 mapped = lib.map_infer(values, f, convert=convert_dtype) 4046 4047 if len(mapped) and isinstance(mapped[0], Series): /usr/local/lib/python3.6/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds) 4043 else: 4044 values = self.astype(object)。 values -> 4045 mapped = lib.map_infer(values, f, convert=convert_dtype) 4046 4047 if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer() pandas._libs.lib.map_infer() 中的 pandas/_libs/lib.pyx

TypeError: 'int' object is not callable类型错误:'int' object 不可调用

Let's try a different approach using isin to create a boolean series and convert to integer:让我们尝试一种不同的方法,使用isin创建 boolean 系列并转换为 integer:

df['loan_status'].isin(['Current','Fully Paid']).astype(int)

I find that the numpy where function is a good choice for these simple column creations while maintaining good speed.我发现 numpy 其中 function 是这些简单列创建的不错选择,同时保持良好的速度。 Something like the below should work:像下面这样的东西应该可以工作:

import numpy as np
df['loan_status_is_great'] = np.where(df['loan_status']=='Current'|
                                      df['loan_status']=='Fully Paid',
                                      1,
                                      0)

暂无
暂无

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

相关问题 使用if语句针对另一列在pandas数据框中创建新列 - Create new column in pandas dataframe using if statement against another column 尝试根据与if语句相关的数据框在熊猫中创建新的数据框列 - Trying to create a new dataframe column in pandas based on a dataframe related if statement 使用一个 Pandas 数据框填充另一个 Pandas 数据框的新列 - Using one pandas dataframe to populate new column in another pandas dataframe 尝试使用Python / pandas基于来自另一个数据帧的列的内部总和来创建新的数据帧 - Trying to create a new dataframe based on internal sums of a column from another dataframe using Python/pandas 在过滤另一列后返回 Pandas dataframe *作为数据帧*的单列 - Returning a single column of Pandas dataframe *as a dataframe* after filtering on another column 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 使用字典为列值过滤pandas数据帧 - Filtering pandas dataframe using dictionary for column values 使用包含字典的 Pandas 列在 DataFrame 中创建新列 - Make new columns in a DataFrame using a pandas column having dictionary inside 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 熊猫使用旧的列名称创建新的数据框 - Pandas make a new dataframe with the old column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM