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

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

在熊貓數據框中創建新列作為分組依據

在熊貓數據框中創建新列作為分組依據

qq_遁去的一_1 2022-07-26 09:24:35
大家好,我需要您的幫助才能在 pandas 數據框中獲得預期的輸出/結果。我有一個包含數據的文件,如下所示:Time/Location    ValueLocation1   Today             3Next day          0Weekend          -6Next week         1Location2   Today             2Next day         -1Weekend           3Next week         2Location3   Today             1Next day          3Weekend 1Next week        -1Location4   Today             3Next day          2Weekend           5Next week         4Location5   Today             4Next day          2Weekend           3Next week         1Location6   Today            -1Next day          3Weekend           3Next week         2并期望輸出如下,它為“位置”創建新列。Location    Time       ValueLocation1   Today       3Location1   Next day    0Location1   Weekend    -6Location1   Next week   1Location2   Today       2Location2   Next day   -1Location2   Weekend     3Location2   Next week   2Location3   Today       1Location3   Next day    3Location3   Weekend     1Location3   Next week  -1Location4   Today       3Location4   Next day    2Location4   Weekend     5Location4   Next week   4Location5   Today       4Location5   Next day    2Location5   Weekend     3Location5   Next week   1Location6   Today      -1Location6   Next day    3Location6   Weekend     3Location6   Next week   2我感謝任何幫助/建議....拜托!
查看完整描述

1 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

如果缺少的值不存在,Value則使用DataFrame.insert替換值以在第一列中丟失并通過 前向填充它們,最后按和列ffill刪除行:DataFrame.dropnarename


df.insert(0, 'Location', df['Time/Location'].mask(df['Value'].notna()).ffill())

df = df.dropna(subset=['Value']).rename(columns={'Time/Location':'Time'})

print (df)

     Location       Time  Value

1   Location1      Today    3.0

2   Location1   Next day    0.0

3   Location1    Weekend   -6.0

4   Location1  Next week    1.0

6   Location2      Today    2.0

7   Location2   Next day   -1.0

8   Location2    Weekend    3.0

9   Location2  Next week    2.0

11  Location3      Today    1.0

12  Location3   Next day    3.0

13  Location3    Weekend    1.0

14  Location3  Next week   -1.0

16  Location4      Today    3.0

17  Location4   Next day    2.0

18  Location4    Weekend    5.0

19  Location4  Next week    4.0

21  Location5      Today    4.0

22  Location5   Next day    2.0

23  Location5    Weekend    3.0

24  Location5  Next week    1.0

26  Location6      Today   -1.0

27  Location6   Next day    3.0

28  Location6    Weekend    3.0

29  Location6  Next week    2.0

另一個想法是測試第一列中的值Series.isin并過濾boolean indexing:


L = ['Today','Next day','Weekend','Next week']

m = df['Time/Location'].isin(L)

df.insert(0, 'Location', df['Time/Location'].mask(m).ffill())

df = df[m].rename(columns={'Time/Location':'Time'})

print (df)

     Location       Time  Value

1   Location1      Today    3.0

2   Location1   Next day    0.0

3   Location1    Weekend   -6.0

4   Location1  Next week    1.0

6   Location2      Today    2.0

7   Location2   Next day   -1.0

8   Location2    Weekend    3.0

9   Location2  Next week    2.0

11  Location3      Today    1.0

12  Location3   Next day    3.0

13  Location3    Weekend    1.0

14  Location3  Next week   -1.0

16  Location4      Today    3.0

17  Location4   Next day    2.0

18  Location4    Weekend    5.0

19  Location4  Next week    4.0

21  Location5      Today    4.0

22  Location5   Next day    2.0

23  Location5    Weekend    3.0

24  Location5  Next week    1.0

26  Location6      Today   -1.0

27  Location6   Next day    3.0

28  Location6    Weekend    3.0

29  Location6  Next week    2.0


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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