0

I have a dataframe with data for each hourly period for a year, and I would like to create a new row with a zero in all the rows that are between 9:00 and 17:00 and in that same row add the data from another row if it is not in this time range

I believe that I want something like;

if '9.00' >= final_df.index <= '17.00':
    do some action
else
    do another action

This is not yet working, the first reason is that at the moment it is missing the full date. Is there some way I can get around that? The first line sort of works if I use;

if '2017-10-16 9.00' >= final_df.index <= '2017-10-16 17.00':

Is there a way I can get around this.

For reference the first 5 data points are;

                       A    B       C   D   E
Timestamp                   
2017-10-15 13:30:00 59.9    17.14   0   1   0
2017-10-15 14:30:00 64.3    17.22   0   1   0
2017-10-15 15:30:00 68.6    17.18   0   1   0
2017-10-15 16:30:00 77.6    17.08   0   1   0
2017-10-15 17:30:00 74.5    16.93   0   1   0
4

1 回答 1

1

您可以使用DatetimeIndex.hour创建一个可以在DataFrame. 对于您给定的数据,假设感兴趣的区域在 15 到 17 之间,并且您想要A在该区域和B外部求和。您可以通过以下方式做到这一点:

In [100]: mask = (df.index.hour > 14) & (df.index.hour < 17)

In [101]: df[mask].A.sum()
Out[101]: 146.2

In [102]: df[~mask].B.sum()
Out[102]: 51.29

编辑:现在添加到问题中的任务恰好也可以通过这种方法轻松解决;假设感兴趣的列是B

In [117]: df['Result'] = ~mask * df.B

In [118]: df
Out[118]:
                        A      B  C  D  E  Result
Timestamp
2017-10-15 13:30:00  59.9  17.14  0  1  0   17.14
2017-10-15 14:30:00  64.3  17.22  0  1  0   17.22
2017-10-15 15:30:00  68.6  17.18  0  1  0    0.00
2017-10-15 16:30:00  77.6  17.08  0  1  0    0.00
2017-10-15 17:30:00  74.5  16.93  0  1  0   16.93
于 2018-12-16T14:25:33.930 回答