Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带列表列的时间序列数据帧_Python_Pandas_List_Time Series - Fatal编程技术网

Python 带列表列的时间序列数据帧

Python 带列表列的时间序列数据帧,python,pandas,list,time-series,Python,Pandas,List,Time Series,我有一个pandas timeseries数据帧,其中一列包含15分钟内5个值的列表。这意味着列表中的每个值每3分钟测量一次 d=[{'time': '09.45', 'value': 0}, {'time': '10.00', 'value': [1, 2, 3, 4, 5]}, {'time': '10.15', 'value': [6, 7, 8, 9, 10]}, {'time': '10.30', 'value': 0}] df = pd.DataFrame(d) p

我有一个pandas timeseries数据帧,其中一列包含15分钟内5个值的列表。这意味着列表中的每个值每3分钟测量一次

d=[{'time': '09.45', 'value': 0},
   {'time': '10.00', 'value': [1, 2, 3, 4, 5]},
   {'time': '10.15', 'value': [6, 7, 8, 9, 10]},
   {'time': '10.30', 'value': 0}]
df = pd.DataFrame(d)
print(df)

我希望每3分钟为每个值创建单独的行。我希望输出如下。如果value列为0,则所有单独的行的值都应为0

time          value
09.48         1
09.51         2
09.54         3
09.57         4
10.00         5
10.03         6
10.06         7
10.09         8
10.12         9
10.15         10
10.18         0
10.21         0
10.24         0
10.27         0
10.30         0

熊猫0.25.0+的解决方案:

#filter out first 0 rows
df = df[df['value'].ne(0).cumsum().gt(0)]
#replace 0 to list filled by 5 times 0
df['value'] = df['value'].apply(lambda x: [0,0,0,0,0] if x == 0 else x)

#convert lists to rows
df = df.explode('value')

#create timedeltas for each 3 minutes
s = pd.to_timedelta(df.groupby(level=0).cumcount(ascending=False) * 3 * 60, unit='s')
#convert string to datetimes, subtract and convert to HH.MM format
df['time'] = pd.to_datetime(df['time'], format='%H.%M').sub(s).dt.strftime('%H.%M')
df = df.reset_index(drop=True)
print (df)
     time value
0   09.48     1
1   09.51     2
2   09.54     3
3   09.57     4
4   10.00     5
5   10.03     6
6   10.06     7
7   10.09     8
8   10.12     9
9   10.15    10
10  10.18     0
11  10.21     0
12  10.24     0
13  10.27     0
14  10.30     0

请提供一个更好的例子,或格式化您的问题,以便理解。看
#filter out first 0 rows
df = df[df['value'].ne(0).cumsum().gt(0)]
#replace 0 to list filled by 5 times 0
df['value'] = df['value'].apply(lambda x: [0,0,0,0,0] if x == 0 else x)

#convert lists to rows
df = df.explode('value')

#create timedeltas for each 3 minutes
s = pd.to_timedelta(df.groupby(level=0).cumcount(ascending=False) * 3 * 60, unit='s')
#convert string to datetimes, subtract and convert to HH.MM format
df['time'] = pd.to_datetime(df['time'], format='%H.%M').sub(s).dt.strftime('%H.%M')
df = df.reset_index(drop=True)
print (df)
     time value
0   09.48     1
1   09.51     2
2   09.54     3
3   09.57     4
4   10.00     5
5   10.03     6
6   10.06     7
7   10.09     8
8   10.12     9
9   10.15    10
10  10.18     0
11  10.21     0
12  10.24     0
13  10.27     0
14  10.30     0