Search code examples
pandasdataframeloopsfor-loopkeyerror

Keyerror when looping over a data frame column


I had a dataset, and I want to create a new data frame from a column in the original one. Chessdata is the original data frame and hizlisatranc is the one that I'm trying to create. However, it raises a keyerror. I couldn't fix it. Can someone please help?

for i in range(len(chessdata)):
    a = chessdata.newtime[i]
    if float(6)<=a<float(25):
        hızlısatranç.append(chessdata.iloc[i])
    else:
        continue

Solution

  • you can filter the df, with values in a range of value1 to value 2, so that only the rows that meet that condition are left. You can assign that to a new df like so:

    df_new = df_old[(df_old['column']>value1) & (df_old['column']<value2)]