为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Pandas DataFrame 创建三个新列

使用 Pandas DataFrame 创建三个新列

人到中年有点甜 2021-06-13 08:22:03
我在下面有一个数据框,并尝试创建更大、更少和计数的三个新列。条件是计算有多少值大于/小于平均值并将它们相加。df =             APPL       Std_1       Std_2       Std_3          Mean       0   ACCMGR      106.8754    130.1600    107.1861    114.750510       1   ACCOUNTS    121.7034    113.4927    114.5482    116.581458       2   AUTH        116.8585    112.4487    115.2700    114.859050def make_count(comp_cols, mean_col):    count_d = {'greater': 0, 'less': 0}    for col in comp_cols:        if col > mean_col:            count_d['greater'] += 1        elif col < mean_col:            count_d['less'] += 1    return count_d['greater'], count_d['less'], (count_d['greater'] + count_d['less'])def apply_make_count(df):    a,b,c,*d= df.apply(lambda row: make_count([row['Std_1'], row['Std_2'], row['Std_3']], row['Mean of Std']), axis=1)    df['greater'],df['less'],df['count']=a,b,capply_make_count(df)但我得到了错误显示:13     df['greater'],df['less'],df['count']=list(zip(a,b,c))ValueError: Length of values does not match length of index我想成为的输出 df =     APPL       Std_1       Std_2       Std_3      Mean  greater less    count0   ACCMGR      106.8754    130.1600    107.1861    114.750510        1    2        31   ACCOUNTS    121.7034    113.4927    114.5482    116.581458        1    2        32   AUTH        116.8585    112.4487    115.2700    114.859050        2    1        3
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

尝试


df['greater'] = (df.iloc[:, 1:4].values > df[['Mean']].values).sum(axis=1)


df['less'] = (df.iloc[:, 1:4].values < df[['Mean']].values).sum(axis=1)


df['count'] = df.iloc[:, 1:4].count(1)



    APPL        Std_1       Std_2       Std_3       Mean       greater  less    count

0   ACCMGR      106.8754    130.1600    107.1861    114.750510  1       2       3

1   ACCOUNTS    121.7034    113.4927    114.5482    116.581458  1       2       3

2   AUTH        116.8585    112.4487    115.2700    114.859050  2       1       3


查看完整回答
反对 回复 2021-06-16
  • 3 回答
  • 0 关注
  • 342 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号