【发布时间】:2020-12-08 21:00:05
【问题描述】:
我有以下盐湖城一个月的风速和风向数据。我想按小时数据进行分组。对于在那一小时内采集的数据,我想要完成两件事:(1) 计算平均风速 (2) 将我定义的函数 (“yamatrino”) 应用于每小时内采集的所有 wind_direction 测量值。
time Station_ID wind_speed wind_direction
0 2019-08-01 00:00:00 UTC WBB 3.48 96.1
1 2019-08-01 00:00:00 UTC UT215 6.54 141.4
2 2019-08-01 00:00:00 UTC MTMET 3.39 67.75
3 2019-08-01 00:00:00 UTC NAA 5.99 154.9
4 2019-08-01 00:00:00 UTC QHW 1.52 107
以下是我编写的代码:(1) 将时间数据转换为日期时间格式,(2) 创建两列,其中包含每小时数据的平均风速和 yamatrino 值。
df['time'] = pd.to_datetime(df['time'], format ='%Y-%m-%d %H:%M:%S UTC')
df.groupby(df['time'].dt.hour)['wind_direction', 'wind_speed'].agg([('yamatrino_value', lambda wind_direction: yamatrino(wind_direction)), ('hourly_velocity_mean', np.mean('wind_speed'))])
错误为“TYPE ERROR: cannot perform reduce with flexible type” 我很困惑如何聚合多列数据。
【问题讨论】:
-
你能先检查一下数据类型吗,好像你的错误指向this problem
-
什么是
hourly_velocity_mean?数据中的方法或列? -
@Parfait 这是我想要创建的列的名称。它是时间平均风速(每小时平均一次)
标签: python pandas dataframe types aggregate