pandas 有条件地创建系列/数据框列 [英] Pandas conditional creation of a series/dataframe column

查看:71
本文介绍了 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

我想向数据框添加另一列(或生成一系列),该列的长度与数据框的长度(=相等的记录/行数)的长度相同,如果Set ='Z'则设置为绿色,如果Set ='Z'则设置为'red'设置=否则.

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 = otherwise.

做到这一点的最佳方法是什么?

What's the best way to do this?

推荐答案

如果只有两个选项可供选择:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆