1

I need to split big table to small table and write it to 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

I need write to the first table all means, where used_at = 4, to second used_at = 5 etc.

I want to get the First table

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

Second

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

And also to used_at = 6

1 Answers1

2

I think you can use groupby with dictionary comprehension:

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

If 4, 5 and 6 are not strings, use print dfs[4]...

EDIT:

If you need store groups to 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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Please check `print dfs[4]`, maybe `4` is not string but number – jezrael Apr 25 '16 at 08:48
  • But if I need to write it not to different `csv`, but different lists in `csv` or `excel` in one file, how can I do it? –  Apr 25 '16 at 09:49
  • I think you cannot write it to one `csv`, but `excel` can works. Please [check](http://stackoverflow.com/questions/14225676/save-list-of-dataframes-to-multisheet-excel-spreadsheet). – jezrael Apr 25 '16 at 10:44
  • How can I save it to list in `excel`? I know, that to `csv`: `df.to_csv()`, but how to different list in `excel`? –  Apr 25 '16 at 11:18
  • Can you ask new question? And add what is desired output in each `sheet`? Thanks. – jezrael Apr 25 '16 at 11:24