带有列表列的熊猫时间序列数据框

我有一个熊猫时间序列数据帧,其中一列包含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)

    time             value
0  09.45                 0
1  10.00   [1,5]
2  10.15  [6,10]
3  10.30                 0

我希望每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
gift1226 回答:带有列表列的熊猫时间序列数据框

熊猫解决方案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] 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
本文链接:https://www.f2er.com/3147012.html

大家都在问