Учитывая первый фрейм данных, есть ли способ с пандами .shift () , .diff () , .replace () или .apply () для выполнения столбца D второго фрейма данных или, если нет, как это можно сделать?
Полезно знать, что при итерациях сверху вниз строки в столбце D остаются True один разстрока в столбце B имеет значение «Истина», и только в том случае, если в столбце «С» не встречается значение «Истина». По сути, это определяет состояние на основе B и С.
A B C
2019-05-04 00:15:00 1 True False
2019-05-04 00:30:00 2 False False
2019-05-04 00:45:00 2 False False
2019-05-04 01:00:00 3 False True
2019-05-04 01:15:00 1 False False
2019-05-04 01:30:00 2 False False
2019-05-04 01:45:00 2 True False
2019-05-04 02:00:00 3 False False
2019-05-04 02:15:00 1 False False
2019-05-04 02:30:00 2 False True
2019-05-04 02:45:00 2 False False
2019-05-04 03:00:00 3 False False
A B C D
2019-05-04 00:15:00 1 True False True
2019-05-04 00:30:00 2 False False True
2019-05-04 00:45:00 2 False False True
2019-05-04 01:00:00 3 False True False
2019-05-04 01:15:00 1 False False False
2019-05-04 01:30:00 2 False False False
2019-05-04 01:45:00 2 True False True
2019-05-04 02:00:00 3 False False True
2019-05-04 02:15:00 1 False False True
2019-05-04 02:30:00 2 False True False
2019-05-04 02:45:00 2 False False False
2019-05-04 03:00:00 3 False False False
Функциональные решения
@ jezrael (импорт numpy, если требуется np)
def determine_state(df,x,y,z):
"""Given a dataframe where columns x and y are Booleans
displaying the entering and exit of a Boolean state, create
a third column that displays the state."""
# use numpy.select with forward filling missing values
df[z] = np.select([df[x], df[y]], [True, False], None)
# replace first Nones by False if exist
df[z] = df[z].ffill().fillna(False)
return df
@ run-out
def determine_state(df,x,y,z):
"""Given a dataframe where columns x and y are Booleans
displaying the entering and exit of a Boolean state, create
a third column that displays the state."""
# set column z to False
df[z] = False
# filter column x for True and set z to True
df.loc[df[x], z] = True
# filter column y for True and set z to False
df.loc[df[y], z] = False
# forward fill on z
df[z] = df[z].ffill(axis=0)
return df