我需要Python帮助:
这是我的数据集的一个示例:
Name Color Emotion Time
Bob NA angry 2018-11-20 16:14:31
Bob NA angry 2018-11-20 16:14:31
Bob red sad 2018-11-20 16:14:31
Bob red neutral 2018-11-20 16:14:31
Bob blue sad 2018-11-20 16:14:32
Bob red sad 2018-11-20 16:14:32
Bob NA neutral 2018-11-20 16:14:33
Bob NA neutral 2018-11-20 16:14:33
Bob NA sad 2018-11-20 16:14:34
Bob blue fear 2018-11-20 16:14:35
请帮助:)谢谢
答案 0 :(得分:0)
要找到first_valid颜色,请使用:
valid_index = df['Color'].first_valid_index()
base_color = df['Color'][valid_index]
关于问题2和3:
for index, row in df.iterrows():
if ((row['Time'] - df['Time'][valid_index]).dt.seconds < 5):
if row['Color'] == np.nan:
row['Color'] = base_color
else:
base_color = row['Color']
valid_index = index