Dataframe index reset considerations

Dataframe index reset considerations

Why do you want to reset the index, because in the extracted weekday data, you want to add a new column: congestion degree, write 1 for index > 2.2, otherwise write 0, but when writing the following code, always report error

df_1_weekday_morning['congestion'] = ''
for i in range(72):
    if df_1_weekday_morning['指数'][i] >= 2.2:
        df_1_weekday_morning['congestion'][i] = 1
    else:
        df_1_weekday_morning['congestion'][i] = 0

So the guess may be that after the weekday is selected, the index is disrupted

insert image description here

So you need to reset the index

But the df.reset_index() function will report an error when using a complex df name, so it will be passed to df first, and the index will be passed back after setting the index

df_3 = df_1_weekday_morning
df_3 = df_3.reset_index(drop=True)
df_1_weekday_morning = df_3

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43697614/article/details/122968309