Python 如何确定数据帧中不同子集的每月运行时间?
我是python新手,正在努力解决以下问题: 我有一个由以下字段组成的数据框架:日期','机器类型','总运行小时'Python 如何确定数据帧中不同子集的每月运行时间?,python,pandas,datetime,dataframe,intervals,Python,Pandas,Datetime,Dataframe,Intervals,我是python新手,正在努力解决以下问题: 我有一个由以下字段组成的数据框架:日期','机器类型','总运行小时' Date Machine TRH 27-02-2018 ... ... 01-03-2018 A 2200 01-03-2018 B 5700 02-03-2018 A 2204 09-03-2018 B 5709 15-03-2018 B 5715 18-03-2018 A
Date Machine TRH
27-02-2018 ... ...
01-03-2018 A 2200
01-03-2018 B 5700
02-03-2018 A 2204
09-03-2018 B 5709
15-03-2018 B 5715
18-03-2018 A 2214
25-03-2018 A 2222
29-03-2018 B 5720
31-03-2018 A 2230
01-04-2018 ... ...
我想确定每种类型机器每月运行的小时数,并将其存储在附加列中
每月小时数可通过下一个公式确定:
当月最后一天的小时数-当月第一天的小时数
对于机器A:(2230-2200)=30
对于机器B:(5720-5700)=20
结果应该是:
Date Machine TRH Monthly TRH
27-02-2018 ... ... ...
01-03-2018 A 2200 30
01-03-2018 B 5700 20
02-03-2018 A 2204 30
09-03-2018 B 5709 20
15-03-2018 B 5715 20
18-03-2018 A 2214 30
25-03-2018 A 2222 30
29-03-2018 B 5720 20
31-03-2018 A 2230 30
01-04-2018 ... ... ...
我不知道如何解决这个问题。我应该使用groupby吗?我应该提到拉巴达吗?我希望你们中的一位能回答我的问题。非常感谢 我猜有人会想出一个比两个groupby更好的方法,但这会管用的 首先对日期进行排序(确保它们是datetime),然后将
First
和last
与groupby机器和月份一起使用(如果数据跨度超过1年,则使用年份)
然后将其带回来,您可以合并
df.merge(temp, on=['Month', 'Year', 'Machine'], how='left').drop(columns=['Month', 'Year'])
# Date Machine TRH Monthly TRH
#0 2018-02-27 ... ... ...
#1 2018-03-01 A 2200.0 30.0
#2 2018-03-02 A 2204.0 30.0
#3 2018-03-18 A 2214.0 30.0
#4 2018-03-25 A 2222.0 30.0
#5 2018-03-31 A 2230.0 30.0
#6 2018-03-01 B 5700.0 20.0
#7 2018-03-09 B 5709.0 20.0
#8 2018-03-15 B 5715.0 20.0
#9 2018-03-29 B 5720.0 20.0
#10 2018-04-01 ... ... ...
编辑
第一种方法速度更快,但还有另一种方法:
'''
Date Machine TRH
01-03-2018 A 2200
01-03-2018 B 5700
02-03-2018 A 2204
09-03-2018 B 5709
15-03-2018 B 5715
18-03-2018 A 2214
25-03-2018 A 2222
29-03-2018 B 5720
31-03-2018 A 2230
'''
# clean up
df = pd.read_clipboard().sort_values('Date')
df.Date = pd.to_datetime(df.Date, dayfirst = True)
df['month'] = df.Date.dt.to_period('M')
df.TRH = pd.to_numeric(df.TRH).copy()
# do it in one go
df.groupby([
'month',
'Machine'
]).nth([
0,
-1
]).groupby([
'month',
'Machine'
]).diff().drop(columns = 'Date').dropna().reset_index().rename(columns = {
'TRH' : 'Monthly TRH'
}).merge(df).drop(columns = 'month')
结果相同,列顺序不同
请格式化您的数据,使其可读。Pandas的第一条规则是不要使用lambdas。Groupby是一个很好的第一个想法。你试过了吗?两种方法都有效!谢谢你!但是,我想将这些值(20和30)存储在一个附加列(每月TRH)中,该列适用于输入数据帧的每一行数据。@L.Scheipers我对其进行了更新,将其恢复到原始的
dataframe
'''
Date Machine TRH
01-03-2018 A 2200
01-03-2018 B 5700
02-03-2018 A 2204
09-03-2018 B 5709
15-03-2018 B 5715
18-03-2018 A 2214
25-03-2018 A 2222
29-03-2018 B 5720
31-03-2018 A 2230
'''
df = pd.read_clipboard()
df.Date = pd.to_datetime(df.Date, dayfirst = True)
df.set_index(pd.DatetimeIndex(df.Date), inplace = True)
df.TRH = pd.to_numeric(df.TRH).copy()
monthly = (df.groupby([pd.Grouper(freq = 'M'), 'Machine']).TRH.last() - df.groupby([pd.Grouper(freq = 'M'), 'Machine']).TRH.first()).to_frame(name = 'Monthly TRH').reset_index()
monthly['month'] = monthly.Date.dt.to_period('M')
monthly.drop('Date', axis = 1, inplace = True)
df['month'] = df.Date.dt.to_period('M')
df.merge(monthly).drop('month', axis = 1)
'''
Date Machine TRH
01-03-2018 A 2200
01-03-2018 B 5700
02-03-2018 A 2204
09-03-2018 B 5709
15-03-2018 B 5715
18-03-2018 A 2214
25-03-2018 A 2222
29-03-2018 B 5720
31-03-2018 A 2230
'''
# clean up
df = pd.read_clipboard().sort_values('Date')
df.Date = pd.to_datetime(df.Date, dayfirst = True)
df['month'] = df.Date.dt.to_period('M')
df.TRH = pd.to_numeric(df.TRH).copy()
# do it in one go
df.groupby([
'month',
'Machine'
]).nth([
0,
-1
]).groupby([
'month',
'Machine'
]).diff().drop(columns = 'Date').dropna().reset_index().rename(columns = {
'TRH' : 'Monthly TRH'
}).merge(df).drop(columns = 'month')