0
def Resample_10mins(df, ZTD_station):
# ensure the time column is in the right format
  df['Date'] = pd.to_datetime(df.Date)

# round to the nearest 10 minute interval
# if you want to floor / ceil the time, you may use 
#`dt.floor` or `dt.ceil` instead of `dt.round`
  df['rounded_to_nearest_10_min'] = df.Date.dt.round('10min')

# to get the mean of all columns
  df = df.groupby('rounded_to_nearest_10_min').agg('mean')

# to get the mean of a specific column
  df = df.groupby('rounded_to_nearest_10_min').agg({ZTD_station: 'mean'})

# Rename date column
  df = df.rename(columns={df.columns[0]: 'Date' })
  # df.rename(columns={'rounded_to_nearest_10_min': 'Date'}, inplace=True)

  return df

I have the following code which I am using to resample my dataframe from 30 sec to 10 mins rate. However, I have noticed that the columns and rows structure changed (compare the 2nd and 3rd dataframes) I want the structure of 2nd one instead of the 3rd one.

Date  GNSS_BIEL
0  2011-01-01 00:00:00   2.247777
1  2011-01-01 00:00:30   2.246933
2  2011-01-01 00:01:00   2.245638
3  2011-01-01 00:01:30   2.244568
4  2011-01-01 00:02:00   2.243413
                               Date
rounded_to_nearest_10_min          
2011-01-01 00:00:00        2.244251
2011-01-01 00:10:00        2.242808
2011-01-01 00:20:00        2.242657
2011-01-01 00:30:00        2.243564
2011-01-01 00:40:00        2.249966
  • So is different ouput if use `df = df.groupby('rounded_to_nearest_10_min').agg('mean')` vs `df = df.groupby('rounded_to_nearest_10_min').agg({ZTD_station: 'mean'})` ? Or different ouput because different input data? – jezrael Sep 10 '21 at 07:31
  • oh i didn't check which one is changing the old format. I will run the code seperately. – Sandy Chkeir Sep 10 '21 at 07:37
  • I just want to take the average of one column. Anyway, I get the same result. Could I change the final result and manipulate the columns to be 2 columns instead of 1 column? – Sandy Chkeir Sep 10 '21 at 07:53
  • Please try to find a more descriptive title for your question. – Erik Sep 16 '21 at 06:45

1 Answers1

0

You can use the built-in resample method:

df.resample('10min', on='Date').mean()

For more details, see this tutorial.

Erik
  • 2,500
  • 2
  • 13
  • 26