在第4章 4-11数据分组技术GroupBy 小节里 执行df_bj.mean() 报错
来源:4-11 数据分组技术GroupBy

慕先生9385916
2023-11-15
4-11-数据分组技术GroupBy
df = pd.read_csv(‘city_weather.csv’)
g = df.groupby(df[‘city’])
df_bj = g.get_group(‘BJ’)
df_bj.mean()
TypeError: Could not convert [‘03/01/201617/01/201631/01/201614/02/201628/02/201613/03/2016’
‘BJBJBJBJBJBJ’] to numeric
谢谢老师
写回答
1回答
-
麦兜搞IT
2024-01-17
试试这个呢
import pandas as pd
# 假设包含日期的列名为'date_column'
df = pd.read_csv('city_weather.csv')
g = df.groupby(df['city'])
df_bj = g.get_group('BJ')
# 拆分拼接的日期并转换为数值型数据
df_bj['date_column'] = df_bj['date_column'].apply(lambda x: [int(date) for date in x.split('/')])
012024-07-25