Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dataframe-基于2个标准计算的列,用于查找日/夜_Python_Pandas_Dataframe - Fatal编程技术网

Python Dataframe-基于2个标准计算的列,用于查找日/夜

Python Dataframe-基于2个标准计算的列,用于查找日/夜,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个日期列和一个切换列,当太阳下山时,这是正确的。 现在,我需要一个计算过的列,告诉我是晚上还是白天 在为真之前是白天,然后是夜晚,直到日期列更改 我创建了一些代码,但我对它不满意,因为它效率低下(for循环) 使用locassignment和groupbyffill和fillna df.loc[df.Toggle, 'Day/Night'] = 'Night' df['Day/Night'] = df['Day/Night'].groupby(df.Date).ffill().filln

我有一个日期列和一个切换列,当太阳下山时,这是正确的。 现在,我需要一个计算过的列,告诉我是晚上还是白天

为真之前是白天,然后是夜晚,直到日期列更改

我创建了一些代码,但我对它不满意,因为它效率低下(for循环)


使用
loc
assignment和groupby
ffill
fillna

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

使用
loc
assignment和groupby
ffill
fillna

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
使用+:

输出

         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
使用+:

输出

         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
         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