如何将计算列按多列分组?

问题描述 投票:0回答:1

您好,我在下面列出了此数据框。

import pandas as pd

db_docks = pd.DataFrame(db_top_25, columns = ['from_station_id', 'to_station_id', 'hour', 'day','dpcapacity_start', 'dpcapacity_end'])

示例数据的图片,因为我无法直接将其嵌入“由于我无法直接将其嵌入图像,因此图像”>>

我需要做的是创建一个新的数据框,该数据框按'小时','日期'和'from_station_id'进行分组。之后,我需要计算'from_station_id'的数量并减去'to_station_id'的数量,这是需要按'hour','day'和'from_station_id'分组的主要列。

[我知道我需要使用.groupby和最有可能的.transform,但是我什至不知道如何开始对该语法进行编码,因此我确实需要帮助。

谢谢。

编辑:

我已经测试过

db_docks = pd.DataFrame(db_top_25.groupby(['from_station_id', 'day',
'hour'])['year'].count())
db_docks.reset_index()

哪个返回此结果,这是正确的,我只需要添加'from_station_id'的实例计数减去'to_station_id'的实例计数之列:this result, which is correct, I just need to add the column of counts of instances of 'from_station_id' minus count of instances of 'to_station_id'

您好,我在下面列出了此数据框。以pd db_docks = pd.DataFrame(db_top_25,columns = ['from_station_id','to_station_id','hour','day','dpcapacity_start','...

python pandas dataframe
1个回答
0
投票

如果您按hour

© www.soinside.com 2019 - 2024. All rights reserved.