简体   繁体   English

如何基于现有列在Python中创建新列

[英]How to create a new column in Python, based on an existing column

Response: 响应:

响应

I need to create a 'Response' column based on an existing 'Time' column. 我需要基于现有的“时间”列创建“响应”列。 My response variable has to display 'No' for Time values from 1s to 60s and from 240s to 300s. 我的响应变量必须在1s到60s和240s到300s的时间值中显示“否”。 And display "Yes' for all the remaining values. 并对所有剩余值显示“是”。

I tried the code below but it simply displays 'No' for all the 'Time' values, disregarding the given condition. 我尝试了下面的代码,但是无论给定条件如何,所有“时间”值都只会显示“否”。

Dataset: 资料集:

数据集

dataset['Y'] = np.where(dataset["Time"] > 60 & (dataset["Time"] < 240 ), 'yes', 'no')
def label(row):
    if row['Time'] >= 1 and row['Time'] < 60:
        return("no")
    elif row['Time'] >= 240 and row['Time'] < 300:
        return("no")
    else:
        return("yes")

dataset['Y'] = dataset.apply(lambda row: label(row), axis=1)

In your code, your condition is wrong so it won't work. 在您的代码中,您的条件是错误的,因此它将不起作用。

I'd be inclined to do the following: 我倾向于做以下事情:

dataset['Y'] = (dataset['Time'] >= 1) & (dataset['Time'] <= 4) | (dataset['Time'] > 5)

Note that this will fill your 'Y' column with bool values. 请注意,这会将布尔值填充到“ Y”列。 If it's paramount that they are Yes/No then you can add change it to 如果最重要的是它们是/否,则可以将其更改为

dataset['Y'] = ((dataset['Time'] >= 1) & (dataset['Time'] <= 4) | (dataset['Time'] > 5)).replace({True: 'Yes', False: 'No'})

Also note that rather than convert your time column into seconds I converted your time intervals into minutes, however you may want to do this different for readability. 还请注意,不是将时间列转换为秒,而是将时间间隔转换为分钟,但是为了提高可读性,您可能需要这样做。

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

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