按日期对数据帧进行排序(d/m/y+小时:分钟:秒)



我正在尝试根据日期(d/m/y+小时:分钟:秒(对列的值进行排序。下面我将向您展示给定数据的格式示例:

>td style="text-align:right;">2020年5月13日11:05:35GlovoappGlovoapp
发起方 价格 日期
XXX 560

以下内容有效。有两个步骤:

  1. 制作掩码以选择右侧行
  2. 然后只对那些行执行groupby和sum

掩码功能:

# Will receive 'actual' datetime from df, and user defined 'start' and 'stop' datetimes.
def in_range(actual, start, stop):
return start <= actual <= stop

然后应用掩码并执行分组:

# creates series of True or False selecting proper rows.
mask = df['date'].apply(in_range, args=(user_start, user_stop))
# Do the groupby and sum on only those rows.
df2 = df.loc[mask].groupby('Initiator').sum()

请注意,user_startuser_stop应该是用户定义的开始和停止日期时间。

你完了!


UPDATE:将方法作为类的一部分包括在内:

class evaluation():
def __init__(self, df):
self.df = df
# Will receive 'actual' datetime from df, and user defined 'start' and 'stop' datetimes. Add 'self' as arg in method.
def in_range(self, actual, start, stop):
return start <= actual <= stop
def evaluate(self):
user_start = pd.to_datetime(input("Enter your start date (yyyy.mm.dd hour:min:second): "))
user_stop = pd.to_datetime(input("Enter your end date (yyyy.mm.dd hour:min:second): "))
# creates series of True or False selecting proper rows.
mask = self.df['Date'].apply(self.in_range, args=(user_start, user_stop)) 
# Do the groupby and sum on only those rows.
amount_init = self.df.loc[mask].groupby("Initiator")["Price"].sum()
print(amount_init)

然后实例化新类的对象:

import pandas as pd
import dateutil.parser as dtp
import evaluation as eval # this is the class we just made
data = {
'Initiator': ['XXX', 'Glovoapp', 'Glovoapp'],
'Price': [560, 250, 250],
'Date': [dtp.parse('13/05/2020 11:05:35'), dtp.parse('12/05/2020 13:07:15'), dtp.parse('13/04/2020 12:09:25')]
}
df = pd.DataFrame(data)
eval_obj = eval.evaluation(df)
eval_obj.evaluate()

相关内容

  • 没有找到相关文章

最新更新