Home > Net >  Turning Nan values into zeroes pandas Python
Turning Nan values into zeroes pandas Python

Time:03-06

I want to turn the nan values into zeroes and get the Expected Output.

import pandas as pd 

data = pd.DataFrame({'Symbol': {4: 'DIS', 5: 'DKNG', 6: 'EXC'}, 
'Number of Buy       s': {4: 1.0, 5: 2.0, 6: 1.0}, 
'Number of Cover     s': {4: nan, 5: 2.0, 6: nan}, 
'Number of Sell      s': {4: 1.0, 5: 1.0, 6: 1.0}, 
'Number of Short     s': {4: nan, 5: 1.0, 6: nan}, 
'Gains/Losses': {4: -47.700000000000045, 5: -189.80000000000018, 6: 11.599999999999909}, 
'Percentage change': {4: -1.9691362018764154, 5: 1.380299604344981, 6: -2.006821924253117}})

Expected Output:

data = pd.DataFrame({'Symbol': {4: 'DIS', 5: 'DKNG', 6: 'EXC'}, 
'Number of Buy       s': {4: 1.0, 5: 2.0, 6: 1.0}, 
'Number of Cover     s': {4: 0, 5: 2.0, 6: 0}, 
'Number of Sell      s': {4: 1.0, 5: 1.0, 6: 1.0}, 
'Number of Short     s': {4: 0, 5: 1.0, 6: 0}, 
'Gains/Losses': {4: -47.700000000000045, 5: -189.80000000000018, 6: 11.599999999999909}, 
'Percentage change': {4: -1.9691362018764154, 5: 1.380299604344981, 6: -2.006821924253117}})

CodePudding user response:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4))
df.iloc[1, 1] = np.nan
df.iloc[1, 2] = np.nan
df.iloc[1, 3] = np.nan
df.iloc[2, 0] = np.nan
df = df.fillna(0)
print(df)

CodePudding user response:

To replace all NaN values of a DataFrame with 0 -

df = df.fillna(0)
  • Related