Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace 0 with blank in dataframe Python pandas

I made the following code that takes out all of the zero's from my df. However when there is a number containing a zero it takes them out as well.

e.g.
3016.2     316.2
   0.235      .235


data_usage_df['Data Volume (MB)'] = data_usage_df['Data Volume (MB)'].str.replace('0', '')

Could you help me to figure out how I do an exact match of the cell that equals 0 and replace it with a blank value.

like image 951
Joe_ft Avatar asked Oct 19 '16 10:10

Joe_ft


People also ask

How do you replace zeros in pandas?

Replace NaN Values with Zero on pandas DataFrame Use the DataFrame. fillna(0) method to replace NaN/None values with the 0 value. It doesn't change the object data but returns a new DataFrame.

How do you remove a zero from a DataFrame in Python?

Remove rows with all zeros using loc[] in Dataframe We are use Dataframe. loc[] attribute to get the rows which are not zeros. The loc[] is used to get the values based on the mentioned index location.


1 Answers

data_usage_df = data_usage_df.astype(str)
data_usage_df['Data Volume (MB)'].replace(['0', '0.0'], '', inplace=True)
like image 81
b2002 Avatar answered Oct 14 '22 01:10

b2002