Pandas 有条件地创建系列/数据框列 [英] Pandas conditional creation of a series/dataframe column
本文介绍了Pandas 有条件地创建系列/数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数据框:
I have a dataframe along the lines of the below:
Type Set
1 A Z
2 B Z
3 B X
4 C Y
我想将另一列添加到与数据帧相同长度的数据帧(或生成一系列)(相同数量的记录/行),如果 'green'
>Set == 'Z' 和 'red'
如果 Set
等于其他任何东西.
I want to add another column to the dataframe (or generate a series) of the same length as the dataframe (equal number of records/rows) which sets a colour 'green'
if Set == 'Z'
and 'red'
if Set
equals anything else.
最好的方法是什么?
推荐答案
如果你只有两种选择:
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
例如
import pandas as pd
import numpy as np
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
print(df)
收益
Set Type color
0 Z A green
1 Z B green
2 X B red
3 Y C red
<小时>
如果您有两个以上的条件,则使用 np.select
.例如,如果您希望 color
为
If you have more than two conditions then use np.select
. For example, if you want color
to be
yellow
当(df['Set'] == 'Z') &(df['Type'] == 'A')
- 否则
blue
当(df['Set'] == 'Z') &(df['Type'] == 'B')
- 否则
purple
当(df['Type'] == 'B')
- 否则
black
,
yellow
when(df['Set'] == 'Z') & (df['Type'] == 'A')
- otherwise
blue
when(df['Set'] == 'Z') & (df['Type'] == 'B')
- otherwise
purple
when(df['Type'] == 'B')
- otherwise
black
,
然后使用
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
conditions = [
(df['Set'] == 'Z') & (df['Type'] == 'A'),
(df['Set'] == 'Z') & (df['Type'] == 'B'),
(df['Type'] == 'B')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')
print(df)
产生的结果
Set Type color
0 Z A yellow
1 Z B blue
2 X B purple
3 Y C black
这篇关于Pandas 有条件地创建系列/数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文