簡體   English   中英

獲取由pandas中的另一列排序的列分區的第一次出現

[英]get the first occurrence partitioned by a column sorted by another column in pandas

我的示例代碼:

import pandas as pd
df = pd.DataFrame({"ID":['1','1','1','2','2'],
                   "LINE":['1','3','2','1','2'],
                   "TYPE":['0','1','1','1','0']})
# print results
print(df.head())

# a function to label the first type 1 for each ID sorted by line
# currently it only filters to type 1
def label (row):
    if row.TYPE == '1' :
        return True

# add the label in the dataframe
df['label'] = df.apply (lambda row: label(row), axis=1)

# print results
print(df.head())

對於按LINE排序的每個唯一ID我想第一次出現TYPE == 1 最終結果應該是:

  ID LINE TYPE label
0  1    1    0  None
1  1    3    1  None
2  1    2    1  True
3  2    1    1  True
4  2    2    0  None

我在這個問題中使用了一個示例,但我實際上正在處理 300 萬個數據行,並且想知道最有效的方法來做到這一點。

使用query過濾TYPE == 1sort_valuesLINE進行排序,最后使用GroupBy.head來獲得第一次出現:

s = df.query('TYPE == "1"').sort_values('LINE').groupby('ID')['TYPE'].head(1)
df['label'] = df.index.isin(s.index)

或者使用drop_duplicates ,這應該更有效:

s = df.query('TYPE == "1"').sort_values('LINE').drop_duplicates('ID')
df['label'] = df.index.isin(s.index)
  ID LINE TYPE  label
0  1    1    0  False
1  1    3    1  False
2  1    2    1   True
3  2    1    1   True
4  2    2    0  False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM