pandas 根据其他列的值创建新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48369929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:04:47  来源:igfitidea点击:

Creating a new column based on the values of other columns

pythonpandas

提问by Mike

I wanted to create a "High Value Indicator" column, which says "Y" or "N" based on two different value columns. I want the new column to have a "Y" when Value_1 is > 1,000 or Value_2 > 15,000. Bellow is the table, the desired output would include the indicator column based on the or condition about.

我想创建一个“高价值指标”列,它根据两个不同的值列显示“Y”或“N”。当 Value_1 > 1,000 或 Value_2 > 15,000 时,我希望新列有一个“Y”。下面是表格,所需的输出将包括基于或条件的指标列。

ID   Value_1     Value_2 
1    100         2500
2    250         6250
3    625         15625
4    1500        37500
5    3750        93750

回答by jezrael

Use numpy.wherewith chained conditions by |for or:

使用numpy.where由链式条件|or

df['High Value Indicator'] = np.where((df.Value_1 > 1000) | (df.Value_2 > 15000), 'Y', 'N')

Or mapby dictionary:

map通过dictionary

df['High Value Indicator'] = ((df.Value_1 > 1000) | (df.Value_2 > 15000))
                                 .map({True:'Y', False:'N'})

print (df)
   ID  Value_1  Value_2 High Value Indicator
0   1      100     2500                    N
1   2      250     6250                    N
2   3      625    15625                    Y
3   4     1500    37500                    Y
4   5     3750    93750                    Y

Timings:

时间:

df = pd.concat([df] * 10000, ignore_index=True)


In [76]: %timeit df['High Value Indicator1'] = np.where((df.Value_1 > 1000) | (df.Value_2 > 15000), 'Y', 'N')
100 loops, best of 3: 4.03 ms per loop

In [77]: %timeit df['High Value Indicator2'] = ((df.Value_1 > 1000) | (df.Value_2 > 15000)).map({True:'Y', False:'N'})
100 loops, best of 3: 4.82 ms per loop

In [78]: %%timeit
    ...: df.loc[((df['Value_1'] > 1000) 
    ...:        |(df['Value_2'] > 15000)), 'High_Value_Ind3'] = 'Y'
    ...: 
    ...: df['High_Value_Ind3'] = df['High_Value_Ind3'].fillna('N')
    ...: 
100 loops, best of 3: 5.28 ms per loop


In [79]: %timeit df['High Value Indicator'] = (df.apply(lambda x: 'Y' if (x.Value_1>1000 or x.Value_2>15000) else 'N', axis=1))
1 loop, best of 3: 1.72 s per loop

回答by Brian

Try using .loc and .fillna

尝试使用 .loc 和 .fillna

df.loc[((df['Value_1'] > 1000) 
       |(df['Value_2'] > 15000)), 'High_Value_Ind'] = 'Y'

df['High_Value_Ind'] = df['High_Value_Ind'].fillna('N')

回答by YOBEN_S

Using map

使用 map

df['High Value Indicator'] =((df.Value_1 > 1000) | (df.Value_2 > 15000)).map({True:'Y',False:'N'})
df
Out[849]: 
   ID  Value_1  Value_2 High Value Indicator
0   1      100     2500                    N
1   2      250     6250                    N
2   3      625    15625                    Y
3   4     1500    37500                    Y
4   5     3750    93750                    Y

回答by Allen

You can also use apply:

您还可以使用申请:

df['High Value Indicator'] = (
     df.apply(lambda x: 'Y' if (x.Value_1>1000 or x.Value_2>15000) else 'N', axis=1)
     )