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

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

根據其他列的查找設置列值

根據其他列的查找設置列值

慕哥9229398 2022-06-28 16:00:17
我有一個DataFrame,其中包含dates、categories和一列,該列顯示該類別是否發生了一次性事件。我想創建一個新列,其中包含事件發生之前的時間,或者某個指標沒有事件,例如負時間。數據集非常大,我想有一個更好的解決方案,而不是使用 Pandas 更好的人會知道的循環暴力破解它!所以,簡而言之,如果我這樣創建我的數據集:import pandas as pd#create example datasetdata = {'categories':['a','b','c']*4,'dates':[i for i in range(4) for j in range(3)],'event':[0]*3*4}#add a couple of eventsdata['event'][4] = 1data['event'][9] = 1df = pd.DataFrame(data)我怎樣才能最好地得到這樣的輸出?   categories  dates  event  time_until0           a      0      0           31           b      0      0           12           c      0      0          -13           a      1      0           24           b      1      1           05           c      1      0          -16           a      2      0           17           b      2      0          -18           c      2      0          -19           a      3      1           010          b      3      0          -111          c      3      0          -1謝謝你的幫助!
查看完整描述

3 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

使用groupby


def f(s):

    s = s.reset_index(drop=True)

    one = s[s.eq(1)]

    if one.empty: return -1

    return -s.index + one.index[0]

df.groupby('categories').event.transform(f)

  categories  dates  event  time_until

0           a      0      0           3

1           b      0      0           1

2           c      0      0          -1

3           a      1      0           2

4           b      1      1           0

5           c      1      0          -1

6           a      2      0           1

7           b      2      0          -1

8           c      2      0          -1

9           a      3      1           0

10          b      3      0          -2

11          c      3      0          -1

請注意,即使在事件發生之后,它也會找到距離。因此,對于以下事件,您將獲得以下輸出


event = [0, 0, 0, 1, 0, 0]

until = [3, 2, 1, 0, -1, -2]

如果您需要使所有負值保持不變-1,那么只需在最后進行調整


df.time_until.where(df.time_until >= -1, -1)


查看完整回答
反對 回復 2022-06-28
?
眼眸繁星

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

替代解決方案:


df.sort_values(by=['categories', 'dates'], ascending=[True, False], inplace=True)

df['tmp'] = df.groupby('categories')['event'].transform('cumsum')

df['time_until'] = df.groupby('categories')['tmp'].transform('cumsum') - 1

df.drop(columns='tmp', inplace=True)

df.sort_values(by=['dates', 'categories'], ascending=[True, True], inplace=True)

輸出:


      categories  dates  event  time_until

0           a      0      0           3

1           b      0      0           1

2           c      0      0          -1

3           a      1      0           2

4           b      1      1           0

5           c      1      0          -1

6           a      2      0           1

7           b      2      0          -1

8           c      2      0          -1

9           a      3      1           0

10          b      3      0          -1

11          c      3      0          -1


查看完整回答
反對 回復 2022-06-28
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

嘗試這樣的事情:


import pandas as pd

import numpy as np


data = {'categories':['a','b','c']*4,

        'dates':[i for i in range(4) for j in range(3)],

        'event':[0, 1, 0]*4}


df = pd.DataFrame(data)

print(df)


# One way

df.loc[df.event == 0, 'Newevents'] = 'Cancelled'

df.loc[df.event != 0, 'Newevents'] = 'Scheduled'


# Another way

conditions = [

    (df['categories'] == "a"),

    (df['categories'] == "b"),

    (df['categories'] == "c")]

choices = ['None', 'Completed', 'Scheduled']

df['NewCategories'] = np.select(conditions, choices, default='black')

print(df)

輸出:


categories  dates  event

0           a      0      0

1           b      0      1

2           c      0      0

3           a      1      0

4           b      1      1

5           c      1      0

6           a      2      0

7           b      2      1

8           c      2      0

9           a      3      0

10          b      3      1

11          c      3      0

categories  dates  event  Newevents NewCategories

0           a      0      0  Cancelled          None

1           b      0      1  Scheduled     Completed

2           c      0      0  Cancelled     Scheduled

3           a      1      0  Cancelled          None

4           b      1      1  Scheduled     Completed

5           c      1      0  Cancelled     Scheduled

6           a      2      0  Cancelled          None

7           b      2      1  Scheduled     Completed

8           c      2      0  Cancelled     Scheduled

9           a      3      0  Cancelled          None

10          b      3      1  Scheduled     Completed

11          c      3      0  Cancelled   


查看完整回答
反對 回復 2022-06-28
  • 3 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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