数据框-基于2个条件的计算列以查找日/夜

时间:2019-11-18 19:36:40

标签: python pandas dataframe

我有一个日期列和一个 Toggle 列,当太阳下山时为True。 现在,我需要一个计算得出的列来说明它是夜晚还是白天。

enter image description here

直到 true 直到 day ,然后直到 date 列更改为晚上。

我创建了一些代码,但由于效率低下(用于循环),我对此不满意。

for i in range(0, len(DF)): 

    j = DF.columns.get_loc('Toggle') 
    k = DF.columns.get_loc('Day/Night')  # This is just a copy of the index which is datetime
    l = DF.columns.get_loc('Date')  

    if DF.iat[i, j] == True:  
        DF['Day/Night'].values[i] = 'Night'

    elif DF.iat[i - 1, k] == 'Night' and DF.iat[i, l] == DF.iat[i - 1, l]:  
        DF['Day/Night'].values[i] = 'Night'

    else:

        DF['Day/Night'].values[i] = 'Day'
Date,Toggle
11/11/2018,FALSE
11/11/2018,FALSE
11/11/2018,TRUE
11/11/2018,FALSE
11/11/2018,FALSE
11/11/2018,FALSE
11/12/2018,FALSE
11/12/2018,FALSE
11/12/2018,FALSE
11/12/2018,TRUE
11/12/2018,FALSE

2 个答案:

答案 0 :(得分:4)

使用groupby + numpy.where

indicator = df.groupby('Date')['Toggle'].cumsum()

df['Day/Night'] = np.where(indicator, 'Night', 'Day')

print(df)

输出

         Date  Toggle Day/Night
0  2018-11-11   False       Day
1  2018-11-11   False       Day
2  2018-11-11    True     Night
3  2018-11-11   False     Night
4  2018-11-11   False     Night
5  2018-11-11   False     Night
6  2018-11-12   False       Day
7  2018-11-12   False       Day
8  2018-11-12   False       Day
9  2018-11-12    True     Night
10 2018-11-12   False     Night

答案 1 :(得分:1)

使用loc分配并按ffillfillna分组

df.loc[df.Toggle, 'Day/Night'] = 'Night'
df['Day/Night'] = df['Day/Night'].groupby(df.Date).ffill().fillna('Day')

Out[588]:
          Date  Toggle Day/Night
0   11/11/2018   False       Day
1   11/11/2018   False       Day
2   11/11/2018    True     Night
3   11/11/2018   False     Night
4   11/11/2018   False     Night
5   11/11/2018   False     Night
6   11/12/2018   False       Day
7   11/12/2018   False       Day
8   11/12/2018   False       Day
9   11/12/2018    True     Night
10  11/12/2018   False     Night