I would like fill a specif column of a dataset with the first not None value. For example, given the df:
col_name
None
None
A
A
B
B
output should be:
col_name
A
A
A
A
B
B
Any help on this, would be very appreciated. Thanks in advance. Carlo
df.update(df.col_name.iloc[:df.col_name.notnull().argmax() + 1].bfill())
df
col_name
0 A
1 A
2 A
3 A
4 B
5 B
Same Idea, but different
i = df.col_name.notnull().argmax()
df.col_name.values[:i] = df.col_name.values[i]
If None values are actually strings as in 'None'
i = df.col_name.ne('None').argmax()
df.col_name.iloc[:i] = df.col_name.iloc[i]
None and not 'None'. Can you run df.col_name.apply(type).iloc[:3] and report back what you get.
df.col_name.bfill()?df.col_name.apply(lambda x: numpy.nan if x==None else x)then do bfill