简体   繁体   English

熊猫使用条件创建系列

[英]Pandas create series using conditional

I'm using the titanic dataset and have a created a series Famsize. 我使用的是泰坦尼克号数据集,并创建了一系列Famsize。 I'd like to create a second series that outputs 'single' if famsize =1, 'small' if 1 < famsize < 5 and 'large' if famsize >=5. 我想创建第二个系列,如果famsize = 1,则输出“单”,如果1 <famsize <5,则输出“小”,如果famsize> = 5,则输出“大”。

   Famsize FamsizeDisc
     1         single
     2         small
     5         large

I've tried using np.where but as I have three outputs I haven't been able to find a solution. 我曾尝试使用np.where,但是由于我有三个输出,因此无法找到解决方案。

Any suggestions? 有什么建议么?

Its called binning so use pd.cut ie 它称为binning因此使用pd.cut

df['new'] = pd.cut(df['Famsize'],bins=[0,1,4,np.inf],labels=['single','small','large'])

Output: 输出:

Famsize FamsizeDisc     new
0        1      single  single
1        2       small   small
2        5       large   large

Either you could create a function which does the mapping: 您可以创建一个执行映射的函数:

def get_sizeDisc(x):
    if x == 1:
        return 'single'
    elif x < 5:
        return 'small'
    elif x >= 5:
        return 'large'

df['FamsizeDisc'] = df.Famsize.apply(get_sizeDisc)

Or you could use .loc 或者您可以使用.loc

df.loc[df.Famsize==1, 'FamsizeDisc'] = 'single'
df.loc[df.Famsize.between(1,5, inclusive = False), 'FamsizeDisc'] = 'small'
df.loc[df.Famsize>=5, 'FamsizeDisc'] = 'large'

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

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