【问题标题】:Manipulating columns and rows操作列和行
【发布时间】:2021-11-06 17:45:32
【问题描述】:
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

我有以下代码,用于将我的数据帧从 30 秒重新采样到 10 分钟。但是,我注意到列和行结构发生了变化(比较第 2 个和第 3 个数据帧)我想要第 2 个而不是第 3 个的结构。

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

【问题讨论】:

  • 如果使用 df = df.groupby('rounded_to_nearest_10_min').agg('mean')df = df.groupby('rounded_to_nearest_10_min').agg({ZTD_station: 'mean'}) 会有不同的输出吗?还是因为输入数据不同导致输出不同?
  • 哦,我没有检查哪一个正在更改旧格式。我将单独运行代码。
  • 我只想取一列的平均值。无论如何,我得到相同的结果。我可以更改最终结果并将列操作为 2 列而不是 1 列吗?
  • 请尝试为您的问题找到一个更具描述性的标题。

标签: python pandas dataframe date pandas-resample


【解决方案1】:

您可以使用内置的重采样方法:

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

更多详情,see this tutorial

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 2015-09-26
    • 2015-09-13
    • 2011-12-07
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多