Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill dataframe Nan values with empty list [] in pandas?

Tags:

python

pandas

nan

This is my dataframe:

          date                          ids
0     2011-04-23  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
1     2011-04-24  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
2     2011-04-25  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
3     2011-04-26  Nan
4     2011-04-27  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
5     2011-04-28  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...

I want to replace Nan with []. How to do that? Fillna([]) did not work. I even tried replace(np.nan, []) but it gives error:

 TypeError('Invalid "to_replace" type: \'float\'',)
like image 740
Alireza Avatar asked Sep 25 '22 13:09

Alireza


People also ask

How do I fill blank cells with NaN in pandas?

Pandas Replace Blank Values with NaN using mask() You can also replace blank values with NAN with DataFrame. mask() methods. The mask() method replaces the values of the rows where the condition evaluates to True.

How do you replace NaN values with an empty list in Python?

Just use [[]]*s. isna(). sum() and you'll be back in business.

How do you replace NaN with nothing pandas?

Use df. replace(np. nan,'',regex=True) method to replace all NaN values to an empty string in the Pandas DataFrame column.


Video Answer


1 Answers

My approach is similar to @hellpanderrr's, but instead tests for list-ness rather than using isnan:

df['ids'] = df['ids'].apply(lambda d: d if isinstance(d, list) else [])

I originally tried using pd.isnull (or pd.notnull) but, when given a list, that returns the null-ness of each element.

like image 55
Nick Edgar Avatar answered Oct 11 '22 11:10

Nick Edgar