How to replace NaN to 0 in one column of excel data extracted with pandas

Viewed 207

I'm a student who only knows a small part of the basics of python, and I'm posting a question to solve a problem in a stats assignment.

https://i.stack.imgur.com/IPaRK.png

I need to extract values from these excel data and output them as graphs in python.

import numpy as np   
import pandas as pd   
from IPython.display import display   

%matplotlib inline


pd.options.display.max_rows = 13
pd.set_option('display.max_columns',750)


file_name = 'C:\last_amb_hour_time.xls'


df = pd.read_excel(file_name, header=6, index_col=0)   
dfdate = pd.read_excel(file_name, header=6, index_col=0, usecols='A')   
dfpm10 = pd.read_excel(file_name, header=6, index_col=0, usecols='C')   
dfpm25 = pd.read_excel(file_name, header=6, index_col=0, usecols='E')   
dfozon = pd.read_excel(file_name, header=6, index_col=0, usecols='G')   
dfno2 = pd.read_excel(file_name, header=6, index_col=0, usecols='I')   
dfco = pd.read_excel(file_name, header=6, index_col=0, usecols='K')   
dfso2 = pd.read_excel(file_name, header=6, index_col=0, usecols='M')   

Currently, the data has been extracted in this way. But many blanks in the excel data are treated as NaN, so the graph cannot be output.

https://i.stack.imgur.com/sayMh.png

like this

I want to replace NaN in one column to 0. However, if I use dfpm10 = df.fillna(0) or replace syntax, the entire excel data is output instead of just one column.

I want to know which command to use to replace NaN data with 0 in one extracted column.

It seems like a simple question, but since I've never learned python properly, I can't solve it even after trying for hours. Sorry.

1 Answers

You should add the name of the column you want to change like this

dfpm10 = df    
dfpm10['COLUMN NAME HERE'] = dfPM10['COLUMN NAME HERE'].fillna(0)
Related