在每个唯一日期的特定时间从 DF 中的列中查找值 [英] Find values from a column in a DF at very specific times for every unique date

查看:59
本文介绍了在每个唯一日期的特定时间从 DF 中的列中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个问题,我得到了一个答案,该答案适用于具有顺序和非缺失数据的一般情况,但不适用于我的情况.我有一个如下所示的 DF.

I asked this question and I got an answer which works for a general case with sequential and non missing data but not for my case specifically. I have a DF that looks as follows.

eventTime       MeteredEnergy Demand RunningHoursLamps 
6/7/2018 0:00   67.728           64  1037.82
6/7/2018 1:00   67.793           64  1038.82
6/7/2018 2:00   67.857           64  1039.82
6/7/2018 3:00   67.922           64  1040.82
6/7/2018 4:00   67.987           64  1041.82
6/7/2018 5:00                    64  1042.82
6/7/2018 6:00                        1043.43
6/7/2018 23:00  68.288
6/8/2018 0:00   67.728           64  1037.82
6/8/2018 23:00  67.793           64  1097.82

我需要一个 DF 来找出eventTime"中每个唯一日期在 0 小时和 23 小时的 RunningHoursLamps 值之间的差异如果数据在 0 小时或 23 小时丢失,则生成的 DF 可能有 NaN

I need a DF that finds the difference between RunningHoursLamps values at hour 0 and hour 23 for each unique date in "eventTime" If data is missing for hour 0 or hour 23, the resultant DF can have NaN

Expected output

    Date        00:00       23:00       Difference 
    6/7/2018    1037.82     NaN         NaN
    6/8/2018    1037.82     1097.82     60

推荐答案

更新:对于那些感兴趣的人:我找到了一种方法来做到这一点.我从 eventTime 列中解析出一个包含日期和小时的单独列,并循环遍历它并在我没有所需 DateTime 的数据时处理异常.谢谢.

Update: For those that are interested: I found a way to do this. I parsed out a separate column with dates and hours from the eventTime column and looped through it and handled exceptions when I did not have data for the required DateTime. Thanks.

#for loop to build the bill dataframe
bill = pd.DataFrame()

for i in range(len(unique_dates)):
    try :
        if i == 0:
            hour0 = np.nan
        else:
            hour0 = df.loc[((df['date'] == unique_dates[i]) & (df['hour'] == 0)),'RunningHoursLamp'].values[0]
    except IndexError:
        hour0 = np.nan

    try :
        hour24 = df.loc[((df['date'] == unique_dates[i+1]) & (df['hour'] == 0)),'RunningHoursLamp'].values[0]
    except IndexError:
        hour24 = np.nan

    temp = pd.DataFrame([[unique_dates[i],hour0,hour24]],columns=['Date','Hour_0','Hour_24'])  
    bill = bill.append(temp,ignore_index=True)

bill

这篇关于在每个唯一日期的特定时间从 DF 中的列中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆