拆分表到小Python

时间:2016-04-25 08:37:10

标签: python csv pandas

我需要将大表拆分为小表并将其写入csv

used_at                                    4                  5                      6
address                          10ruslake.ru 1c.ru    vk.com  yandex.ru  youtube.com  facebook.com   
ID                                                                     
0025977ab2998580d4559af34cc66a4e          0.0   0.0      152      465          45           56       
00c651e018cbcc8fe7aa57492445c7a2          0.0   0.0      23       213          354          44   
0120bc30e78ba5582617a9f3d6dfd8ca          0.0   0.0      0        100          109          0

我需要写入第一个表格,其中used_at = 4,第二个used_at = 5等等。

我想获得第一张表

used_at                                    4  
address                          10ruslake.ru 1c.ru
ID
0025977ab2998580d4559af34cc66a4e          0.0   0.0
00c651e018cbcc8fe7aa57492445c7a2          0.0   0.0
0120bc30e78ba5582617a9f3d6dfd8ca          0.0   0.0

第二

used_at                                        5  
address                                 vk.com    yandex.ru
ID
0025977ab2998580d4559af34cc66a4e          152      465
00c651e018cbcc8fe7aa57492445c7a2          23       213
0120bc30e78ba5582617a9f3d6dfd8ca          0        100

还有used_at = 6

1 个答案:

答案 0 :(得分:2)

我认为您可以将groupby与字典理解结合使用:

dfs = {i: g for i,g in df.groupby(axis=1, level=0)}


print dfs['4']
                                            4      
                                 10ruslake.ru 1c.ru
0025977ab2998580d4559af34cc66a4e          0.0   0.0
00c651e018cbcc8fe7aa57492445c7a2          0.0   0.0
0120bc30e78ba5582617a9f3d6dfd8ca          0.0   0.0

print dfs['5']
                                      5          
                                 vk.com yandex.ru
0025977ab2998580d4559af34cc66a4e    152       465
00c651e018cbcc8fe7aa57492445c7a2     23       213
0120bc30e78ba5582617a9f3d6dfd8ca      0       100

print dfs['6']
                                           6             
                                 youtube.com facebook.com
0025977ab2998580d4559af34cc66a4e          45           56
00c651e018cbcc8fe7aa57492445c7a2         354           44
0120bc30e78ba5582617a9f3d6dfd8ca         109            0

如果456不是strings,请使用print dfs[4] ...

编辑:

如果您需要将groups存储到list

dfs = [g for i, g in df.groupby(axis=1, level=0)]

print dfs[0]
used_at                                     4      
address                          10ruslake.ru 1c.ru
ID                                                 
0025977ab2998580d4559af34cc66a4e          0.0   0.0
00c651e018cbcc8fe7aa57492445c7a2          0.0   0.0
0120bc30e78ba5582617a9f3d6dfd8ca          0.0   0.0

print dfs[1]
used_at                               5          
address                          vk.com yandex.ru
ID                                               
0025977ab2998580d4559af34cc66a4e    152       465
00c651e018cbcc8fe7aa57492445c7a2     23       213
0120bc30e78ba5582617a9f3d6dfd8ca      0       100

print dfs[2]
used_at                                    6             
address                          youtube.com facebook.com
ID                                                       
0025977ab2998580d4559af34cc66a4e          45           56
00c651e018cbcc8fe7aa57492445c7a2         354           44
0120bc30e78ba5582617a9f3d6dfd8ca         109            0