Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill in a blank dataframe column with all 0 values using Python

I have a Python dataframe with a column called 'avg_snow' that is completely blank when I output the table to a CSV file.

I want to fill in the blank rows with the value 0 (datatype float). I have tried the following, but it gives me an error.

merged_left_1 = merged_left_1['avg_snow'].replace('', 0, inplace = True)

This is the error message I get:

TypeError: 'NoneType' object is not subscriptable

like image 234
PineNuts0 Avatar asked Jan 08 '17 12:01

PineNuts0


Video Answer


1 Answers

You can do that in multiple ways. I am creating a dummy dataframe to show you how it works:

df = pd.DataFrame(data=[None,None,None],columns=['a'])

One way is:

df['a'] = 0 # Use this if entire columns values are None.

Or a better way to do is by using pandas' fillna:

df.a.fillna(value=0, inplace=True) # This fills all the null values in the columns with 0.
like image 92
Vishnu Subramanian Avatar answered Sep 22 '22 00:09

Vishnu Subramanian