I am trying to make the value of Action timeline = Dialogue timeline, only if Action comes before dialogue:
x = []
for i in range(len(df)):
if df['Type'][i] == "Action" < df['Type'][i] == "Dialouge":
x.append(df['Timeline'])[enter image description here][1]
CodePudding user response:
You can set a boolean mask on the conditions, then set the rows of column Timeline
fulfilling the conditions to the corresponding values of next rows by .loc
and .shift()
, as follows:
(Note the spelling/mispelling if the word Dialouge
(rather than Dialogue
) in the sample data)
mask = (df['Timeline'].isna() | (df['Timeline'] == '')) & (df['Type'] == "Action") & (df['Type'].shift(-1) == "Dialouge")
df.loc[mask, 'Timeline'] = df['Timeline'].shift(-1)
Data Input:
Type Timeline
0 Action NaN
1 Dialouge 00:05:54
2 Action NaN
3 Dialouge 00:06:06
4 Action NaN
5 Dialouge NaN
Output:
print(df)
Type Timeline
0 Action 00:05:54
1 Dialouge 00:05:54
2 Action 00:06:06
3 Dialouge 00:06:06
4 Action NaN
5 Dialouge NaN