0

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

5
  • What about df.col_name.bfill()? Commented Sep 1, 2017 at 16:53
  • I tried and does not work...it makes all the col_name with None value. Commented Sep 1, 2017 at 16:56
  • df.col_name.apply(lambda x: numpy.nan if x==None else x) then do bfill Commented Sep 1, 2017 at 16:58
  • how can I add inplace=True? to the df.col_name.bfill() Commented Sep 1, 2017 at 17:02
  • no, it does not work Commented Sep 1, 2017 at 17:05

2 Answers 2

2
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]
Sign up to request clarification or add additional context in comments.

17 Comments

how can I add inplace=True to your suggestion? Many Thanks,
I see. I am running it...just a sec
The first suggestion does not work for me. I try the other one.
I got the following error: IndexError: index 173 is out of bounds for axis 0 with size 109
Are you sure those are None and not 'None'. Can you run df.col_name.apply(type).iloc[:3] and report back what you get.
|
0

You can convert the value to a numpy array by the dataframe.values variable, and then loop through it, comparing to numpy.nan, setting the value when true

eg.

colName = df['col_name'].values

colName[numpy.isnan(colName)] = 'A'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.