简体   繁体   English

基于多列创建新列 pandas

[英]Creating new column based on multiple columns pandas

I'm trying to create categories based on multiple columns in pandas but it is taking forever to run so i'm not sure it is correct.我正在尝试基于 pandas 中的多个列创建类别,但它需要永远运行,所以我不确定它是否正确。 I left for 30 mns and was still running so stopped it.我离开了 30 分钟,但仍在运行,所以停止了它。 I'm trying to create a new column based on several other columns (in my actual data it is about 15 cols).我正在尝试基于其他几列创建一个新列(在我的实际数据中它大约是 15 列)。 However when I try on a smaller dataset it is very quick.但是,当我尝试使用较小的数据集时,它非常快。 Any suggestions?有什么建议么?

other_cols = ['col1', 'col2', 'col3', 'col4', 'col5']


def labels(row):
    if ((row['col 6'] > 1) & (row[other_cols] < 1)).all():
        return 'Yes'
    if ((row['col 6'] >1) & (row['col 7'] >1) & (row[other_cols] <1)).all():
        return 'Maybe'
    if ((row['col 6'] <1) & (row['col 7']>1) & (row[other_cols] <1)).all():
        return 'no'

df['category'] = df.apply(lambda row: labels(row), axis=1)

You can try that maybe:您可以尝试一下:

ther_cols = ['col1', 'col2', 'col3', 'col4', 'col5']


def labels(row):
    elif ((row['col 6'] > 1) & (row[other_cols] < 1)).all():
        row['category'] = 'Yes'
    elif ((row['col 6'] >1) & (row['col 7'] >1) & (row[other_cols] <1)).all():
        row['category'] = 'Maybe'
    elif ((row['col 6'] <1) & (row['col 7']>1) & (row[other_cols] <1)).all():
        row['category'] = 'no'
    else:
        row['category'] = ''

df = df.apply(labels, axis=1)

What is the size of your dataset?你的数据集的大小是多少?

I'm sorry i can not comment I am still new here对不起,我不能评论我还是新来的

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

相关问题 Pandas:基于多个不同的列创建列 - Pandas: Creating column based on multiple different columns 基于多准则和多列的PANDAS新列 - PANDAS NEW COLUMN BASED ON MULTIPLE CRITERIA AND COLUMNS 基于多列创建新列 - Creating a new column based on multiple columns 熊猫-基于2列和一个单独的测试列创建2个新列 - Pandas - creating 2 new columns based on 2 columns and a separate test column 根据其他行和列的多个条件在数据框中创建新列? 包括空行? - 蟒蛇/熊猫 - Creating a new column in dataframe based on multiple conditions from other rows and columns? Including rows that are null? - Python/Pandas 根据多个列中至少有一个是否包含列表中的值这一事实创建新列(Python,熊猫) - creating new column based on the fact whether at least 1 of multiple columns contains value from the list (Python, pandas) 基于python pandas中其他列的值创建新列 - Creating a new column based on values from other columns in python pandas Pandas - 根据对其他列进行的条件数学创建新列 - Pandas - Creating a new column based on conditional math done on other columns Pandas dataframe:根据其他列的数据创建新列 - Pandas dataframe: Creating a new column based on data from other columns 比较 2 个 pandas 数据框列并根据值是否相同创建新列 - Comparing 2 pandas dataframe columns and creating new column based on if the values are same or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM