根据唯一的列组合将数据帧分为多个数据帧

时间:2020-03-29 00:54:33

标签: python pandas dataframe

我有以下数据框:

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

可以吗?

3 个答案:

答案 0 :(得分:1)

通过groupby运行字典理解。您可以在groupby:split-apply-combine页面的Pandas文档中阅读更多内容:

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

答案 1 :(得分:1)

另一种方法是将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

答案 2 :(得分:0)

您是对的,只是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