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

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

如何在 pandas 中按多個列值進行分組并應用 ifelse 來插補/計算值

如何在 pandas 中按多個列值進行分組并應用 ifelse 來插補/計算值

UYOU 2023-10-18 16:33:07
我有一個如下所示的數據框 dfNode COMMODITY_CODE DAY Capacity_Case  Capacity_Delivery case_ratio deliveries_ratio  window_count7014.0      SCFZ    1   26610.0         12.0                0.357854    0.354839.            37014.0      SCFZ    2   25551.0         11.0                0.457945    0.423077             37014.0      SCFZ    3   30669.0         13.0                0.283379    0.258621             37030.0      SCDD    1   34244.0         16.0                0.316505    0.300000             47030.0      SCDD    2   25954.0         13.0                0.236513    0.232558             4我想按 Node、DAY、COMMODITY_CODE 進行分組,并應用 ifelse 函數來估算空記錄的值。這里我的條件如下:對于組(節點、DAY、COMMODITY_CODE)如果 Delivery_ratio 為空,那么我想用組的mean(delivery_ratio) 替換并將其分配給delivery_ratio_filled如果 case_ratio 為空,那么我想用組的mean(case_ratio) 替換并將其分配給case_ratio_filled如果對于組(Node, DAY, COMMODITY_CODE),Delivery_ratio_filled 為 null,則為其分配 1/window_count 值case_ratio_filled 為 null,則為其分配 1/window_count我已經使用 dplyr 包在 R 中輕松完成了此任務,我基本上希望使用 pandas 在 Python 中實現相同的功能。df %>%group_by(Node, DAY_OF_WK, COMMODITY_CODE) %>%  mutate(delivery_ratio_filled = ifelse(!is.na(delivery_ratio),                               delivery_ratio,                                mean(delivery_ratio)),         case_ratio_filled = ifelse(!is.na(case_ratio),                               case_ratio,                                mean(case_ratio))) %>%  mutate(delivery_ratio_filled = ifelse(!is.na(delivery_ratio_filled),                               delivery_ratio_filled,                               1.0 / window_count),         case_ratio_filled = ifelse(!is.na(case_ratio_filled),                               case_ratio_filled,                               1.0 / window_count))
查看完整描述

2 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

不幸的是,示例輸入數據不包含na將被計算值替換的值(或大于一項的組)。因此,新列是原始列的簡單副本。

第一個條件可以測試并np.where應用于每一行transform

df[['delivery_ratio_filled','case_ratio_filled']] = (

? ? df.groupby(['Node', 'DAY', 'COMMODITY_CODE'])[['deliveries_ratio','case_ratio']]

? ? ? .transform(

? ? ? ? lambda x: np.where(x.isna(), x.mean(), x)))

第二個條件不需要分組


df['delivery_ratio_filled'] = (

? np.where(df['delivery_ratio_filled'].isna(),

? ? ? ? ? ?1 / df['window_count'],

? ? ? ? ? ?df['delivery_ratio_filled']))

df['case_ratio_filled'] = (

? np.where(df['case_ratio_filled'].isna(),

? ? ? ? ? ?1 / df['window_count'],

? ? ? ? ? ?df['case_ratio_filled']))

df

出去:


? ? ?Node COMMODITY_CODE? ...? delivery_ratio_filled? case_ratio_filled

0? 7014.0? ? ? ? ? ?SCFZ? ...? ? ? ? ? ? ? ?0.354839? ? ? ? ? ?0.357854

1? 7014.0? ? ? ? ? ?SCFZ? ...? ? ? ? ? ? ? ?0.423077? ? ? ? ? ?0.457945

2? 7014.0? ? ? ? ? ?SCFZ? ...? ? ? ? ? ? ? ?0.258621? ? ? ? ? ?0.283379

3? 7030.0? ? ? ? ? ?SCDD? ...? ? ? ? ? ? ? ?0.300000? ? ? ? ? ?0.316505

4? 7030.0? ? ? ? ? ?SCDD? ...? ? ? ? ? ? ? ?0.232558? ? ? ? ? ?0.236513


查看完整回答
反對 回復 2023-10-18
?
夢里花落0921

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

也可以用dplyrpython的方式實現:


>>> from datar.all import f, tribble, group_by, mutate, if_else, is_na, mean

>>>?

>>> df = tribble(

...? ? ?f.Node, f.COMMODITY_CODE, f.DAY, f.Capacity_Case,? f.Capacity_Delivery, f.case_ratio, f.delivery_ratio,? f.window_count,

...? ? ?7014.0, "SCFZ",? ? ? ? ? ?1,? ? ?26610.0,? ? ? ? ? 12.0,? ? ? ? ? ? ? ? 0.357854,? ? ?0.354839,? ? ? ? ? 3,

...? ? ?7014.0, "SCFZ",? ? ? ? ? ?2,? ? ?25551.0,? ? ? ? ? 11.0,? ? ? ? ? ? ? ? 0.457945,? ? ?0.423077,? ? ? ? ? 3,

...? ? ?7014.0, "SCFZ",? ? ? ? ? ?3,? ? ?30669.0,? ? ? ? ? 13.0,? ? ? ? ? ? ? ? 0.283379,? ? ?0.258621,? ? ? ? ? 3,

...? ? ?7030.0, "SCDD",? ? ? ? ? ?1,? ? ?34244.0,? ? ? ? ? 16.0,? ? ? ? ? ? ? ? 0.316505,? ? ?0.300000,? ? ? ? ? 4,

...? ? ?7030.0, "SCDD",? ? ? ? ? ?2,? ? ?25954.0,? ? ? ? ? 13.0,? ? ? ? ? ? ? ? 0.236513,? ? ?0.232558,? ? ? ? ? 4,

... )? ??

>>>?

>>>?

>>> df >> \

...? ? ?group_by(f.Node, f.DAY, f.COMMODITY_CODE) >> \

...? ? ?mutate(delivery_ratio_filled = if_else(~is_na(f.delivery_ratio),

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? f.delivery_ratio,?

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mean(f.delivery_ratio)),

...? ? ? ? ? ? case_ratio_filled = if_else(~is_na(f.case_ratio),

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? f.case_ratio,?

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mean(f.case_ratio))) >> \

...? ? ?mutate(delivery_ratio_filled = if_else(~is_na(f.delivery_ratio_filled),

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? f.delivery_ratio_filled,

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1.0 / f.window_count),

...? ? ? ? ? ? case_ratio_filled = if_else(~is_na(f.case_ratio_filled),

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? f.case_ratio_filled,

...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1.0 / f.window_count))

? ? ? ?Node COMMODITY_CODE? ? ?DAY? Capacity_Case? Capacity_Delivery? case_ratio? delivery_ratio? window_count? delivery_ratio_filled? case_ratio_filled

? <float64>? ? ? ?<object> <int64>? ? ? <float64>? ? ? ? ? <float64>? ?<float64>? ? ? ?<float64>? ? ? ?<int64>? ? ? ? ? ? ? <float64>? ? ? ? ? <float64>

0? ? 7014.0? ? ? ? ? ?SCFZ? ? ? ?1? ? ? ? 26610.0? ? ? ? ? ? ? ?12.0? ? 0.357854? ? ? ? 0.354839? ? ? ? ? ? ?3? ? ? ? ? ? ? ?0.354839? ? ? ? ? ?0.357854

1? ? 7014.0? ? ? ? ? ?SCFZ? ? ? ?2? ? ? ? 25551.0? ? ? ? ? ? ? ?11.0? ? 0.457945? ? ? ? 0.423077? ? ? ? ? ? ?3? ? ? ? ? ? ? ?0.423077? ? ? ? ? ?0.457945

2? ? 7014.0? ? ? ? ? ?SCFZ? ? ? ?3? ? ? ? 30669.0? ? ? ? ? ? ? ?13.0? ? 0.283379? ? ? ? 0.258621? ? ? ? ? ? ?3? ? ? ? ? ? ? ?0.258621? ? ? ? ? ?0.283379

3? ? 7030.0? ? ? ? ? ?SCDD? ? ? ?1? ? ? ? 34244.0? ? ? ? ? ? ? ?16.0? ? 0.316505? ? ? ? 0.300000? ? ? ? ? ? ?4? ? ? ? ? ? ? ?0.300000? ? ? ? ? ?0.316505

4? ? 7030.0? ? ? ? ? ?SCDD? ? ? ?2? ? ? ? 25954.0? ? ? ? ? ? ? ?13.0? ? 0.236513? ? ? ? 0.232558? ? ? ? ? ? ?4? ? ? ? ? ? ? ?0.232558? ? ? ? ? ?0.236513


[Groups: Node, DAY, COMMODITY_CODE (n=5)]


查看完整回答
反對 回復 2023-10-18
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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