繁体   English   中英

基于多列创建新列 pandas

[英]Creating new column based on multiple columns pandas

我正在尝试基于 pandas 中的多个列创建类别,但它需要永远运行,所以我不确定它是否正确。 我离开了 30 分钟,但仍在运行,所以停止了它。 我正在尝试基于其他几列创建一个新列(在我的实际数据中它大约是 15 列)。 但是,当我尝试使用较小的数据集时,它非常快。 有什么建议么?

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)

您可以尝试一下:

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)

你的数据集的大小是多少?

对不起,我不能评论我还是新来的

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM