gpt4 book ai didi

python - 如何拆分具有多个批处理的数据框中的每一行并为每个批处理打印出不同的行

转载 作者:太空宇宙 更新时间:2023-11-03 15:43:35 24 4
gpt4 key购买 nike

我将 csv 文件转换为数据框。我需要根据单行中存在的批处理数量将数据框中的每一行拆分为不同的行。

这里是输入我只包含 2 行

A002,R051,02-00-00,05-21-11,00:00:00,REGULAR,003169391,001097585,05-21-11,04:00:00,REGULAR,003169415,001097588,05-21-11,08:00:00,REGULAR,003169431,001097607,05-21-11,12:00:00,REGULAR,003169506,001097686,05-21-11,16:00:00,REGULAR,003169693,001097734,05-21-11,20:00:00,REGULAR,003169998,001097769,05-22-11,00:00:00,REGULAR,003170119,001097792,05-22-11,04:00:00,REGULAR,003170146,001097801              
A002,R051,02-00-00,05-22-11,08:00:00,REGULAR,003170164,001097820,05-22-11,12:00:00,REGULAR,003170240,001097867,05-22-11,16:00:00,REGULAR,003170388,001097912,05-22-11,20:00:00,REGULAR,003170611,001097941,05-23-11,00:00:00,REGULAR,003170695,001097964,05-23-11,04:00:00,REGULAR,003170701,001097964,05-23-11,08:00:00,REGULAR,003170746,001098069,05-23-11,12:00:00,REGULAR,003170897,001098378

最佳答案

设置

import pandas as pd
from io import StringIO

txt = """A002,R051,02-00-00,05-21-11,00:00:00,REGULAR,003169391,001097585,05-21-11,04:00:00,REGULAR,003169415,001097588,05-21-11,08:00:00,REGULAR,003169431,001097607,05-21-11,12:00:00,REGULAR,003169506,001097686,05-21-11,16:00:00,REGULAR,003169693,001097734,05-21-11,20:00:00,REGULAR,003169998,001097769,05-22-11,00:00:00,REGULAR,003170119,001097792,05-22-11,04:00:00,REGULAR,003170146,001097801
A002,R051,02-00-00,05-22-11,08:00:00,REGULAR,003170164,001097820,05-22-11,12:00:00,REGULAR,003170240,001097867,05-22-11,16:00:00,REGULAR,003170388,001097912,05-22-11,20:00:00,REGULAR,003170611,001097941,05-23-11,00:00:00,REGULAR,003170695,001097964,05-23-11,04:00:00,REGULAR,003170701,001097964,05-23-11,08:00:00,REGULAR,003170746,001098069,05-23-11,12:00:00,REGULAR,003170897,001098378
"""

df = pd.read_csv(StringIO(txt), header=None, index_col=[0, 1, 2])

Pandas

使用% + // + 堆栈

idx = pd.RangeIndex(len(df.columns))
df.columns = [idx % 5, idx // 5]
df.stack().rename_axis([None] * 4)

0 1 2 3 4
A002 R051 02-00-00 0 05-21-11 00:00:00 REGULAR 3169391 1097585
1 05-21-11 04:00:00 REGULAR 3169415 1097588
2 05-21-11 08:00:00 REGULAR 3169431 1097607
3 05-21-11 12:00:00 REGULAR 3169506 1097686
4 05-21-11 16:00:00 REGULAR 3169693 1097734
5 05-21-11 20:00:00 REGULAR 3169998 1097769
6 05-22-11 00:00:00 REGULAR 3170119 1097792
7 05-22-11 04:00:00 REGULAR 3170146 1097801
0 05-22-11 08:00:00 REGULAR 3170164 1097820
1 05-22-11 12:00:00 REGULAR 3170240 1097867
2 05-22-11 16:00:00 REGULAR 3170388 1097912
3 05-22-11 20:00:00 REGULAR 3170611 1097941
4 05-23-11 00:00:00 REGULAR 3170695 1097964
5 05-23-11 04:00:00 REGULAR 3170701 1097964
6 05-23-11 08:00:00 REGULAR 3170746 1098069
7 05-23-11 12:00:00 REGULAR 3170897 1098378

关于python - 如何拆分具有多个批处理的数据框中的每一行并为每个批处理打印出不同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41957348/

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