TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas.fillna()

Pandas.fillna() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.fillna()

We can use the fillna() function to fill the null values in the dataset.

Syntax:

DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)

Parameters:

  • value: It is a value that is used to fill the null values, alternately a Series/dict/DataFrame.
  • method: A method that is used to fill the null values in the reindexed Series.
  • axis: It takes int or string value for rows/columns. Axis along which we need to fill missing values.
  • inplace: If it is True, it fills values at an empty place.
  • limit: It is an integer value that specifies the maximum number of consecutive forward/backward NaN value fills.
  • downcast: It takes a dict that specifies what to downcast like Float64 to int64.

Returns:

It returns an object in which the missing values are being filled.

Example1:

import pandas as pd
# Create a dataframe
info = pd.DataFrame(data={'x':[10,20,30,40,50,None]})
print(info)
# Fill null value to dataframe using 'inplace'
info.fillna(value=0, inplace=True)
print(info)

Output

       x
0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5     NaN
       x
0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5      0.0

Example2:

The below code is responsible for filling the DataFrame that consist some NaN values.

import pandas as pd
# Create a dataframe
info = pd.DataFrame([[np.nan,np.nan, 20, 0],
[1, np.nan, 4, 1],
[np.nan, np.nan, np.nan, 5],
[np.nan, 20, np.nan, 2]],
columns=list('ABCD'))
info

Output

    A    B     C    D
0  NaN  NaN   20.0  0
1  1.0  NaN   4.0   1
2  NaN  NaN   NaN   5
3  NaN  20.0  NaN   2

Example3:

In below code, we have used the fillna function to fill in some of the NaN values only.

info = pd.DataFrame([[np.nan,np.nan, 20, 0],
[1, np.nan, 4, 1],
[np.nan, np.nan, np.nan, 5],
[np.nan, 20, np.nan, 2]],
columns=list('ABCD'))
info
info.fillna(0)
info.fillna(method='ffill')
values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
info.fillna(value=values)
info.fillna(value=values, limit=1)

Output

    A    B     C    D
0  0.0  1.0   20.0  0
1  1.0  NaN   4.0   1
2  NaN  NaN   2.0   5
3  NaN  20.0  NaN   2





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf