gpt4 book ai didi

python - 将表拆分为小型 Python

转载 作者:行者123 更新时间:2023-12-01 03:59:35 32 4
gpt4 key购买 nike

我需要将大表拆分为小表并将其写入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

最佳答案

我认为你可以使用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不是字符串,则使用print dfs[4] ...

编辑:

如果您需要将存储到列表:

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

关于python - 将表拆分为小型 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835591/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com