简体   繁体   English

根据 Pandas 中的整数值将 DataFrame 行拆分为多行

[英]Split a DataFrame row into multiple rows based on integer value in Pandas

Is there way we can split a row into multiple rows based on an integer cap value?有没有办法根据整数上限值将一行拆分为多行? I have a dataframe as below我有一个如下所示的数据框

sys_df = pd.DataFrame([{'dateTime': '2020-11-12 17:45:00', 'timeTakenInSeconds': 650, 'id':'xyz'}])
Index指数 dateTime约会时间 timeTakenInSeconds timeTakenInSecs id ID
0 0 2020-11-12 17:45:00 2020-11-12 17:45:00 650 650 xyz xyz

I am trying to split the above row into 3 rows of previous 5 minute intervals like below.我试图将上面的行分成 3 行,前 5 分钟间隔如下。

Index指数 dateTime约会时间 timeTakenInSeconds timeTakenInSecs id ID
0 0 2020-11-12 17:45:00 2020-11-12 17:45:00 300 300 xyz xyz
1 1 2020-11-12 17:40:00 2020-11-12 17:40:00 300 300 xyz xyz
2 2 2020-11-12 17:35:00 2020-11-12 17:35:00 50 50 xyz xyz

Do we have any pandas builin utils to achieve this?我们是否有任何 Pandas bulin utils 来实现这一目标?

You can build your own method.您可以构建自己的方法。 A lead can be:潜在客户可以是:

import pandas as pd
from datetime import datetime, timedelta
origin_data = {'dateTime': '2020-11-12 17:45:00', 'timeTakenInSeconds': 650, 'id':'xyz'}

def splitter(origin_data, interval=0):
    data=[]
    to_sec = interval*60
    current_time = datetime.fromisoformat(origin_data['dateTime'])
    for item in range((origin_data['timeTakenInSeconds']//to_sec)):
        data.append({'dateTime': str(current_time),
                     'timeTakenInSeconds': to_sec, 'id':'xyz'})
        current_time -= timedelta(seconds=to_sec)
    reminder = origin_data['timeTakenInSeconds'] - (origin_data['timeTakenInSeconds']//to_sec)*to_sec
    if reminder:
        data.append({'dateTime': str(current_time),
                     'timeTakenInSeconds': reminder, 'id': 'xyz'})
    return data


print(pd.DataFrame(splitter(origin_data, interval=5)))

Outputs:输出:

              dateTime  timeTakenInSeconds   id
0  2020-11-12 17:45:00                 300  xyz
1  2020-11-12 17:40:00                 300  xyz
2  2020-11-12 17:35:00                  50  xyz

Note:笔记:

You can also use:您还可以使用:

pd.date_range(end=datetime.fromisoformat(origin_data['dateTime']), periods=3, freq='5min')

To split the date as you wish.根据需要拆分日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM