亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能得到負數百分比和最負數排序?

我怎樣才能得到負數百分比和最負數排序?

胡子哥哥 2022-07-26 10:18:12
我想出了一種方法來進行分組并根據兩個字段獲取計數:df.groupby(['brand','result']).size()df.groupby(['brand','result']).count()這會產生相同的結果。我的數據現在看起來像這樣。Johnson's Baby Powder   negative         21                          neutral          5                          positive         121Estee Lauder            negative         7                          positive         23Calvin Klein            negative         10                          neutral          3                          positive         29 我想得到每個品牌的結果百分比,就像這樣。Johnson's Baby Powder   negative    21  0.142857143                        neutral     5   0.034013605                        positive    121 0.823129252Estee Lauder            negative    7   0.233333333                        positive    23  0.766666667Calvin Klein            negative    10  0.238095238                        neutral     3   0.071428571                        positive    29  0.69047619不過,最終,我只想顯示“結果”負值 > 20% 的“品牌”。所以,我想看看這個(以及其他符合業務邏輯規則的品牌)。Estee Lauder            negative    7   0.233333333                        positive    23  0.766666667我怎樣才能做到這一點?
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

嘗試


x = df.groupby(['brand'])['result'].value_counts(normalize=True)

樣本數據輸出


>>> y = x.loc[(x.index.get_level_values(1) == 'negative')]


>>> y[y>0.2]

airline         airline_sentiment

American        negative             0.710402

Delta           negative             0.429793

Southwest       negative             0.490083

US Airways      negative             0.776862

United          negative             0.688906

Virgin America  negative             0.359127

Name: airline_sentiment, dtype: float64


>>> y[y>0.2].index.get_level_values(0)

Index(['American', 'Delta', 'Southwest', 'US Airways', 'United',

       'Virgin America'],

      dtype='object', name='airline')


查看完整回答
反對 回復 2022-07-26
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

添加到@Vishnudev 的答案,使用:


print(df[df.groupby(['brand'])['result'].value_counts(normalize=True).ge(0.5).tolist()])

輸出:


          brand    result  number

3  Estee Lauder  negative       7

4  Estee Lauder  positive      23


查看完整回答
反對 回復 2022-07-26
  • 2 回答
  • 0 關注
  • 68 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號