简体   繁体   English

使用过去 24 小时的总和创建新列

[英]creating new column with the sum of the past 24 hours

For the following dataframe: df_data, is there a way to make a new column that counts the nr of vehicles of the past 24 hours or just of the previous day?对于以下 dataframe:df_data,有没有办法创建一个新列来计算过去 24 小时或前一天的车辆数量?

df_data = {'day_of_year' : [1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2], 'nr_of_vehicles' : [254,154,896,268,254,501,840,868,654,684,684,681,632,468,987,134,336,119,874,658,121,254,154,896,268,254,501,840,868,654,684,684,681,632,468,987,134,336,119,874,658,121,268,254,501,840,868,654], 'hour' : [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]} 

Visual representation (nr_of_vehicles is counted per hour):视觉表示(nr_of_vehicles 每小时计数):

数据框

I thought of grouping the data by day_of_year by using the following我想通过使用以下方法按 day_of_year 对数据进行分组

df_data_day = df_data.groupby('day_of_year').agg({'nr_of_vehicles': 'sum'})

but I don't know how I could assign it correctly to the column, because the are more rows in the original dataframe.但我不知道如何将其正确分配给列,因为原始 dataframe 中有更多行。

You were not far: you had just to use transform instead of agg :你不远:你只需要使用transform而不是agg

df_data_day = df_data.groupby('day_of_year')['nr_of_vehicles'].transform('mean')

You can even directly add a new column:您甚至可以直接添加一个新列:

df_data['nr_by_day'] = df_data.groupby('day_of_year')['nr_of_vehicles'].transform('mean')

BTW: I used your proposed code which computes the average, when your title says sum...顺便说一句:当你的标题说总和时,我使用了你提出的计算平均值的代码......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用ExchangeLib在过去24小时内退回电子邮件 - Trying to return emails within past 24 hours using ExchangeLib 如果在过去 24 小时内没有找到电子邮件,Python exchangelib 创建警报 - Python exchangelib create alert if no email is found the past 24 hours 使用 Tweepy 从过去 24 小时内随机生成的推文 - Random Tweets from past 24 hours using Tweepy 在创建新文件之前,让python在24小时内每小时一次写入文件一次,持续24小时 - Getting python to write to a file once per hour for 24 hours before creating a new file 使用其他列的总和创建新列 - Creating new column with sum of other columns 让 Telebot 将新用户静音 24 小时 - Make telebot mute new user for 24 hours 传输新文件或最近 24 小时内编辑过的文件 - Transfer files that are new or edited in the last 24 hours Sqlalchemy:如何每 24 小时更新一次列? - Sqlalchemy: How to update a column every 24 hours? Scrapy:如何在if语句中获取过去24小时内发布的项目? - Scrapy: How to get items that has been posted in the past 24 hours in an if statement? 尝试收集过去24小时内的所有推文,并将它们放入CSV文件中 - Trying to Gather all tweets from the past 24hours and put them into a CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM