简体   繁体   中英

pandas time series dataframe with list columns

I have a pandas timeseries dataframe where a column contains list of 5 values over a period of 15 mins. That means each value inside the list is measured in every 3 mins.

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, 2, 3, 4, 5]
2  10.15  [6, 7, 8, 9, 10]
3  10.30                 0

I would like to have separate rows for each value for every 3 mins. I want the output like below. If the value column is 0, then it should be 0 for all the separate rows.

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

Solution for pandas 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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