[Python] Looping through a dataframe column to replace NaN values with values that aren't null:

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    enter image description here

    The three columns on the left are day month, and year. I am trying to fill in NaN values in the last column which I am calling 'C'. For each week of each month there is one non-empty value in the last column, and I would like to assign the NaN values with the non-empty value.

    So far I have tried doing it with the first week with the following code:

    for year in range(2013, 2023):
    for month in range(1, 13):
    for day in range(1, 8):
    df.loc[pd.isnull(df['C']), 'C'] = df.loc[(df['year'] == year) & (df['month'] == month) & (df['day'] == 3), 'C']

    Continue reading...

Compartilhe esta Página