Reputation: 964
I am working on some data where data is recorded every few seconds for an entire day. I have to resample this data by taking the mean for every minute.
For example here is my data
Date Time Recorded_data
2019-01-01 00:00:20 0.20
2019-01-01 00:00:25 0.25
2019-01-01 00:00:28 0.16
2019-01-01 00:00:32 0.26
2019-01-01 00:00:36 0.28
2019-01-01 00:00:45 0.26
2019-01-01 00:00:48 0.24
2019-01-01 00:00:56 0.24
2019-01-01 00:01:00 0.18
...
I want to resample the data above as
Date Time Recorded_data
2019-01-01 00:00:00 0.23
...
I want to take the mean for all data for every minute.
I tried resample with pandas but that just converts all data in the Recorded_data column to Nan
this is the code i used to resample the data
df.Recorded_data.resample('min').mean()
I tried the answer in this link, but it does not seem to work
Upvotes: 0
Views: 251
Reputation: 862641
Use DataFrame.resample
by T
for minutes with mean
with parameter on
, then DatetimeIndex
is not necesary:
df['datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
df = df.resample('T', on='datetime').Recorded_data.mean().reset_index(name='Recorded_data')
print (df)
datetime Recorded_data
0 2019-01-01 00:00:00 0.23625
1 2019-01-01 00:01:00 0.18000
Upvotes: 1
Reputation: 38415
If Date and Time are separate columns, combine them first and convert to date time. You can then resample on the datetime column,
df['datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
df.set_index('datetime').resample('H').Recorded_data.mean().reset_index(name = 'Recorded_data')
You get
datetime Recorded_data
0 2019-01-01 0.23
Upvotes: 2