'Python: Index not increasing in for-loop

trying to loop through a column in a df and if there is a NaN in the i'th cell value, I want to replace that NaN with the j'th element in list 'l'.

So when j=0 and we initiate the loop, and at i=12, there is a NaN, this NaN is replaced with l1[0]. Next round j=1, at i = 21 is the next NaN in the column, we replace this by l[1] and so on.

I tried to print the i and j indexes, but it never goes to the next j index, so it replaces all the NaNs in the column with element l[0]. So j is not increasing every time a NaN value is replaced with the j in question.

Any thoughts? Appreciate the help!

l = [i for i in range(0,11)]
data = {'column': [1,2,3,4,5,6,7,8,9,10,np.nan, np.nan, np.nan, 11,12,13]}
df = pd.DataFrame(data)

for j in range(len(l)):
    for i in range(len(df["column"])):
            if pd.isnull(df["column"].iloc[i]):
                df.at[i,"column"] = l[j]
                print(f"i, j = {i, j}")

Every round I print I see i increasing but j equals 0. Note: the column length is larger than the length of the list I created.



Solution 1:[1]

Try with .fillna:

df["column_name"] = df["column_name"].fillna(df["column_name"].isnull().cumsum().apply(lambda x: l[x]))

Or with .loc:

df.loc[df["column_name"].isnull(),"column_name"] = l[:df["column_name"].isnull().sum()]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 not_speshal