Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: Insert a zero in a DataFrame cell

I have a Dataframe with time series, whose values are presented below:

01/05/2023      25.1  25.9  25.1  
01/05/2023   1  25.1  25.2  25.0 
01/05/2023   2  24.7  25.1  24.7  
01/05/2023   3  24.7  24.8  NaN  

In the cell above number 1, located at [0,1], should have a zero. How is the best way to do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That depends on what pandas considers you have there:

If you have a blank space, i. e. ' ', then you should probably try pd.replace()

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1':['',1,2,3],
                   'col2':[25.9,25.2,25.1,24.8],
                   'col3':[25.1, 25.0, 24.7, np.nan]})
print(df)

returns:

  col1  col2  col3
0       25.9  25.1
1    1  25.2  25.0
2    2  25.1  24.7
3    3  24.8   NaN

If you then do

df.replace('', 0, inplace=True)
print(df)

you get

   col1  col2  col3
0     0  25.9  25.1
1     1  25.2  25.0
2     2  25.1  24.7
3     3  24.8   NaN

Another method of doing the same thing is using pd.replace without the inplace argument (be careful to assign the df again then df = df.replace('',0).

If it’s a NaN (which I doubt seeing the other NaN in your dataframe) you could try with pd.fillna()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading