Hi I want to replace blanks in my dataframe as 0. When I run this:
list(df['col'])
I get list of values['12345','78910',...., '' ,'5678', '12344', '', '7654']. How can I replace these '' missing values with 0. I want the whole df to be replaced by 0 when there is a blank like this ''.
CodePudding user response:
As you have strings, I imagine that the best is to convert to_numeric
, then fillna
with 0 the invalid/missing values:
df['col'] = pd.to_numeric(df['col'], errors='coerce').fillna(0, downcast='infer')
For the whole DataFrame:
df = df.apply(pd.to_numeric, errors='coerce').fillna(0, downcast='infer')
Example output:
col
0 12345
1 78910
2 0
3 5678
4 12344
5 0
6 7654
Used input:
df = pd.DataFrame({'col': ['12345','78910','' ,'5678', '12344', '', '7654']})
CodePudding user response:
You can try this:
>>>
data = {'a': ['12345', '78910', '', '5678', '12344', '', '7654'],
'b': ['295', '7', '', '8', '', '37484', '901']}
df = pd.DataFrame(data)
print(df)
a b
0 12345 295
1 78910 7
2
3 5678 8
4 12344
5 37484
6 7654 901
>>>
out = df.replace('', 0)
print(out)
a b
0 12345 295
1 78910 7
2 0 0
3 5678 8
4 12344 0
5 0 37484
6 7654 901