9951 explained code solutions for 126 technologies


python-pandasHow to change nan values to zero


import pandas as pd
import numpy as np

phoneDataSet = {
  'Phone': ['iPhone 5', 'iPhone 6', 'iPhone8', 'Galaxy S9', 'Galaxy Note 10'],
  'Phone ID.': [12, 9, np.nan, 78, np.nan],
  'Phone Price': [204, np.nan, 501, 800, np.nan]
}

data = pd.DataFrame(phoneDataSet)
data = data.replace(np.nan, 0)ctrl + c
import pandas as pd

load Pandas module

import numpy as np

load Numpy module for Python

phoneDataSet

sample dict data to create dataframe from

pd.DataFrame

creates Pandas DataFrame object

.replace(

search given value and replace with specified value

np.nan

value to search for


Usage example

import pandas as pd
import numpy as np

phoneDataSet = {
  'Phone': ['iPhone 5', 'iPhone 6', 'iPhone8', 'Galaxy S9', 'Galaxy Note 10'],
  'Phone ID.': [12, 9, np.nan, 78, np.nan],
  'Phone Price': [204, np.nan, 501, 800, np.nan]
}

data = pd.DataFrame(phoneDataSet)
data = data.replace(np.nan, 0)

print(data)
output
            Phone  Phone ID.  Phone Price
0        iPhone 5       12.0        204.0
1        iPhone 6        9.0          0.0
2         iPhone8        0.0        501.0
3       Galaxy S9       78.0        800.0
4  Galaxy Note 10        0.0          0.0