按日期和另一个字段分组。

huangapple go评论65阅读模式
英文:

Group by date and another field

问题

我觉得我可能忽略了显而易见的问题,但似乎无法使用"group by"函数对我的数据进行分组。为了澄清,我有一个站点 ID日期,其他列是状态 ID、可用自行车和可用停车位。我想要按日期和站点ID对数据进行分组,并计算自行车和停车位的平均值,但似乎不起作用。

我的代码如下:

Final_Df = df.groupby([df['date'].dt.date, 'station_id']).mean()

类型错误显示“Series 对象是不可变的,因此无法进行哈希运算。”

我是 Python 新手 - 有任何帮助吗?

尝试将 df['station_id'] 替换为只是 station_id,但没有成功。提前感谢您的帮助。

英文:

I think I am missing the obvious but I can't seem to group my data using the group by function. To clarify I have a station ID and the date with the other columns being status Id, bikes available and docks available. I want to group my data by the date and station ID with the bikes available and docks available calculated as the mean but it doesn't seem to work.

My code is as follows:

Final_Df=df.groupby([df['date'].dt.date],df['station_id']).mean()

The type error shows 'Series objects are not mutable, this they cannot be hashed.

I am new to python - any help?

Tried replacing df['station_id'] with just station_id but to no avail. Thanks in advance

答案1

得分: 0

您的groupby参数应该是一个单一的列表[df['date'].dt.date, df['station_id']](注意右边的括号是])。

df = pd.DataFrame({'Date_Time': pd.date_range('10/1/2001 10:00:00', periods=3, freq='10H'),
                   'B': [4, 5, 6],
                   'station_id': [1, 1, 2]})

df.groupby([df['Date_Time'].dt.date, df['station_id']]).mean()

结果为

                         B
Date_Time  station_id     
2001-10-01 1           4.5
2001-10-02 2           6.0

英文:

Your groupby argument should be a single list [df['date'].dt.date ,df['station_id']] (note the closing bracket to the right of ['station_id'].

df = pd.DataFrame({'Date_Time': pd.date_range('10/1/2001 10:00:00', periods=3, freq='10H'),
                   'B':[4,5,6],
                   'station_id':[1, 1, 2]})
                   
df.groupby([df['Date_Time'].dt.date, df['station_id']]).mean()

results in

                         B
Date_Time  station_id     
2001-10-01 1           4.5
2001-10-02 2           6.0

huangapple
  • 本文由 发表于 2023年6月15日 05:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定