根据布尔条件添加计算行

时间:2017-09-14 17:45:20

标签: python pandas

假设我有一个表格如下:

Name | weekNumber  | Status | HoursON
-----|-------------|--------|----------
 M1  |       1     |  False |   12
 M1  |       1     |  True  |    3
 M1  |       1     |  False |    2
 M1  |       2     |  False |   17
 M1  |       2     |  True  |    4

如果weekNumber的结尾以' False'结尾?状态,我需要追加一行,复制所有列值,除了' HoursON'和'状态。 "营业时间"价值将从24减去,状态'将切换为“真实”。换句话说,我更喜欢这个表格如下:

Name | weekNumber  | Status | HoursON
-----|-------------|--------|----------
 M1  |       1     |  True  |   12
 M1  |       1     |  True  |    3
 M1  |       1     |  False |    2
 M1  |       1     |  True  |   22    <------ (24 -2 )
 M1  |       2     |  False |   17
 M1  |       2     |  True  |    4    <------ No change

我正在考虑按如下方式执行操作:

def uptimeOffset(x):
   return 24 - x

for elem in df.weekNumber:
    if df.Status[len(df.Status)-1] == 'False':
        df['HoursON'] = df.apply(uptimeOffset(df.HoursON))

我收到以下错误:

TypeError: ("'Series' object is not callable", 'occurred at index HoursON')

您能否告诉我我们如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以创建check_last函数来检查每个组的最后一行,使用Status添加新行HoursONappend

In [733]: def check_last(x):
     ...:     last_row = x.iloc[-1]
     ...:     if last_row.Status:
     ...:         return x
     ...:     last_row.Status = True
     ...:     last_row.HoursON = 24 - last_row.HoursON
     ...:     return x.append(last_row)

In [734]: df.groupby('weekNumber').apply(check_last).reset_index(drop=True)
Out[734]:
  Name  weekNumber  Status  HoursON
0   M1           1   False       12
1   M1           1    True        3
2   M1           1   False        2
3   M1           1    True       22
4   M1           2   False       17
5   M1           2    True        4

答案 1 :(得分:0)

或者您可以尝试

df1=df.groupby('weekNumber').nth(-1)[~df.groupby('weekNumber').nth(-1).Status]
df1['HoursON']=24-df1['HoursON']
df1['Status']=True
pd.concat([df,df1.reset_index().set_index(df1.index+len(df)-1)],axis=0)\
    .sort_index().sort_values('weekNumber').reset_index(drop=True)

Out[387]: 
   HoursON  Name   Status  weekNumber
0       12   M1     False           1
1        3   M1      True           1
2        2   M1     False           1
3       22   M1      True           1
4       17   M1     False           2
5        4   M1      True           2