Replace NaN with 0 in pandas DataFrame in Python (2 Examples) | Substitute by Zeros | All/One Column

Published: 13 January 2023
on channel: Statistics Globe
563
20

How to exchange NaN values in a pandas DataFrame by 0 in the Python programming language. More details: https://statisticsglobe.com/replace-n...
Python code of this video:

import pandas as pd # Import pandas

data = pd.DataFrame({'x1':[float('NaN'), 0, 1, float('NaN'), 1, 0], # Create example DataFrame
'x2':[1, 7, float('NaN'), 5, 3, 1],
'x3':[10, 11, 12, float('NaN'), float('NaN'), 13]})
print(data) # Print example DataFrame

data_new1 = data.fillna(0) # Substitute NaN in all columns
print(data_new1) # Print DataFrame with zeros

data_new2 = data.copy() # Create copy of input DataFrame
data_new2['x1'] = data_new2['x1'].fillna(0) # Substitute NaN in single column
print(data_new2) # Print DataFrame with zeros in single column

Follow me on Social Media:
Facebook - Statistics Globe Page:   / statisticsglobecom  
Facebook - R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook - Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn - Statistics Globe Page:   / statisticsglobe  
LinkedIn - R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn - Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com


Watch video Replace NaN with 0 in pandas DataFrame in Python (2 Examples) | Substitute by Zeros | All/One Column online without registration, duration hours minute second in high quality. This video was added by user Statistics Globe 13 January 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 563 once and liked it 20 people.