比较基于内部日间时间(小时)的值,并将答案返回到同一只 pandas DataFame中的新列 [英] Compare values based on inner day time (hours), and return the answer to a new column in the same pandas DataFame

查看:109
本文介绍了比较基于内部日间时间(小时)的值,并将答案返回到同一只 pandas DataFame中的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据小时日期时间索引比较不同行之间的值。

I'm trying to compare values in between different rows based on a hour datetime index.

例如:如果10:00值大于11:00在同一天取值,然后将布尔值返回到新列。

For example: if 10:00 Value is greater than 11:00 Value on the same day, then return the Boolean value to a new column.

初始数据:

                    Value
Time                                                                           
2018-11-09 10:00:00    25
2018-11-09 11:00:00    45
2018-11-09 12:00:00    15
2018-11-09 13:00:00    95
2018-11-12 10:00:00    85
2018-11-12 11:00:00    35
2018-11-12 12:00:00    55 
2018-11-12 13:00:00    65

dataframe snippet:
import pandas as pd
data = [25,45,15,95,85,35,55,65]
index = ['2018-11-09 10:00:00','2018-11-09 11:00:00','2018-11-09 12:00:00','2018-11-09 13:00:00'
         ,'2018-11-12 10:00:00','2018-11-12 11:00:00','2018-11-12 12:00:00','2018-11-12 13:00:00']

df = pd.DataFrame(data,index,columns=['Value'])
df.index = pd.to_datetime(df.index)

所需的输出:

                    Value   10h-Val 11h-Val 12h-Val 13h-Val 12h-Val>11h-Val? 
Time                                                                           
2018-11-09 10:00:00    25       25         
2018-11-09 11:00:00    45               45
2018-11-09 12:00:00    15                       15                   False
2018-11-09 13:00:00    95                               95
2018-11-12 10:00:00    85         85            
2018-11-12 11:00:00    35               35 
2018-11-12 12:00:00    55                       55                    True
2018-11-09 13:00:00    65                               65                                    


推荐答案

添加这些列的最快方法如下:

the quickest method for adding those columns would be structured like this:

df.index = pd.to_datetime(df.index)
df['10h-val'] = df.loc[df.index.hour == 10].Value
df['12h-Val>11h-Val?'] = ''

您会注意到pandas将使用标题i创建一个新名称在左侧的方括号中,将值设置为等于值列的值,其中小时等于
10,我们需要填充比较列。

you'll notice pandas will create a new name with the header in the square brackets on the left and set the value equal to the column Value where the hour equals 10 and we'll need to populate the comparison column.

对于比较列,我们将遍历数据框的行并更新与基准小时匹配的行:

for the comparison column, we'll loop through the rows of our dataframe and update those matching a the base hour:

base_hr = 12
comp_hr = 11

for index, row in df.iterrows():
    if index.hour == base_hr:
        row = row.copy()
        base_value = row.Value
        comp_value = df.loc[((row.date == df.index.date) 
            & (df.index.hour == comp_hr))]['Value'].values[0]        
        df.loc[index,'12h-Val>11h-Val?'] = base_value > comp_value

df = df.drop(columns='date')

然后打印以获得所需的结果:

then print to get the result you're looking for:

print(df)

这篇关于比较基于内部日间时间(小时)的值,并将答案返回到同一只 pandas DataFame中的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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