簡體   English   中英

根據唯一的列組合將數據框拆分為多個數據框

[英]Split data frame into multiple data frames based on unique column combinations

我有以下數據框:

import pandas as pd

units = [1, 1, 1, 5, 5, 5]
locations = [30, 30, 30, 32, 32, 32]
timestamps = [1, 2, 3, 1, 2, 3]
quantities = [1, 5, 3, 10, 35, 39]
data = {'units': units, 'locations': locations, 'timestamps': timestamps,
        'quantities': quantities}
df = pd.DataFrame(data=data)

看起來像這樣:

🐍 >>> df
   units  locations  timestamps  quantities
0      1         30           1           1
1      1         30           2           5
2      1         30           3           3
3      5         32           1          10
4      5         32           2          35
5      5         32           3          39

我需要從單位和位置的所有獨特組合中獲取數據框列表,即使用df.groupby(['units', 'locations']) 最終結果應該是這樣的:

(1, 30)
   timestamps  quantities
0           1           1
1           2           5
2           3           3

(5, 32)
   timestamps  quantities
3           1          10
4           2          35
5           3          39

請問這可能嗎?

通過 groupby 運行字典理解。 您可以在 Pandas doc for groupby:split-apply-combine頁面上閱讀更多相關信息:

d = {name:group.filter(['timestamps','quantities']) 
     for name, group in df.groupby(['units','locations'])}

#print(d.keys())
#dict_keys([(1, 30), (5, 32)])

print(d[(1,30)])

    timestamps  quantities
0       1           1
1       2           5
2       3           3

 print(d[(5,32)])

  timestamps    quantities
3       1          10
4       2          35
5       3          39

另一種方法是將 dict comp 與groupbyconcat

d = pd.concat(({combo : data for combo,data in df.groupby(['units','locations'])}))

print(d)

        units  locations  timestamps  quantities
1 30 0      1         30           1           1
     1      1         30           2           5
     2      1         30           3           3
5 32 3      5         32           1          10
     4      5         32           2          35
     5      5         32           3          39

你是對的,它只是 groupby:

cols = ['units','locations']
for k, d in df.drop(cols, axis=1).groupby([df[c] for c in cols]):
    print(k)
    print(d)

輸出:

(1, 30)
   timestamps  quantities
0           1           1
1           2           5
2           3           3
(5, 32)
   timestamps  quantities
3           1          10
4           2          35
5           3          39

暫無
暫無

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

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