Home > Mobile >  Replace na in output with zeroes in pandas python
Replace na in output with zeroes in pandas python

Time:08-28

I wanted to replace the 'na' value in the output with 0.0. Those are not NaN values. It had 'na' value in the csv file. I tried every method I thought I could to replace na with 0.0 but to no avail. Here is the output:

            Brunei Darussalam   Indonesia   Malaysia   Philippines   Thailand   Viet Nam   Myanmar   Japan   ...  Austria   Scandinavia   CIS & Eastern Europe     USA   Canada   Australia   New Zealand   Africa
                                                                                                             ...

 1978 Jan                  na          na         na            na         na         na        na   18,652  ...       na          1,881                    433   8,362    1,328      28,421         3,612      
587
 1978 Feb                  na          na         na            na         na         na        na   20,394  ...       na          2,112                    514   8,251    1,434      13,982         2,521      
354
 1978 Mar                  na          na         na            na         na         na        na   20,136  ...       na          2,183                    472   9,901    1,662      16,536         2,727      
405
 1978 Apr                  na          na         na            na         na         na        na   13,508  ...       na          1,590                    405  11,782    1,586      16,499         3,197      
736
 1978 May                  na          na         na            na         na         na        na   14,472  ...       na          1,245                    431  13,448    2,025      20,690         5,130      
514

What the output is supposed to look like:

            Brunei Darussalam   Indonesia   Malaysia   Philippines   Thailand   Vietnam   Myanmar   Japan   ...  Austria   Scand0.0via   CIS & Eastern Europe     USA   Canada   Australia   New Zealand   Africa
                                                                                                             ...

 1978 Jan                 0.0         0.0        0.0           0.0        0.0        0.0       0.0   18,652  ...      0.0          1,881                    433   8,362    1,328      28,421         3,612      
587
 1978 Feb                 0.0         0.0        0.0           0.0        0.0        0.0       0.0   20,394  ...      0.0          2,112                    514   8,251    1,434      13,982         2,521      
354
 1978 Mar                 0.0         0.0        0.0           0.0        0.0        0.0       0.0   20,136  ...      0.0          2,183                    472   9,901    1,662      16,536         2,727      
405
 1978 Apr                 0.0         0.0        0.0           0.0        0.0        0.0       0.0   13,508  ...      0.0          1,590                    405  11,782    1,586      16,499         3,197      
736
 1978 May                 0.0         0.0        0.0           0.0        0.0        0.0       0.0   14,472  ...      0.0          1,245                    431  13,448    2,025      20,690         5,130      
514

I tried converting with fillna, but it did not work. I even tried numpy to convert it to zeroes, but it did not work. Here is my code below

import pandas as pd
import numpy as np

df = pd.read_csv('Int_Monthly_Visitor.csv', header=0, index_col=0, na_values=0.0)

print(df.head(5).replace(np.nan, 0.0))

So far I was not able to make it work. I can give the CSV file if needed.

CodePudding user response:

Since you are reading CSV file, you can directly pass na to na_values parameter of read_csv function (currently you are passing 0.0), then you can call fillna(0) either for entire dataframe, or for the columns of your choice:

pd.read_csv('Int_Monthly_Visitor.csv', header=0, index_col=0, na_values=['na']).fillna(0)

  Brunei Darussalam  Indonesia  Malaysia  ...  Australia  New Zealand  Africa
0          1978 Jan        0.0       0.0  ...      1,328       28,421   3,612
1          1978 Feb        0.0       0.0  ...      1,434       13,982   2,521
2          1978 Mar        0.0       0.0  ...      1,662       16,536   2,727
3          1978 Apr        0.0       0.0  ...      1,586       16,499   3,197
4          1978 May        0.0       0.0  ...      2,025       20,690   5,130
[5 rows x 16 columns]

CodePudding user response:

It could be possible that 'na' is actually a string so you could probably replace it like this

df.replace('na',0.0,inplace=True)

CodePudding user response:

Please try fillna() function. Thanks

df.fillna(0)
  • Related