Use of na_values parameter in read_csv() function of Pandas in Python
Last Updated :
20 Mar, 2024
read_csv() is an important pandas function to read CSV files. But there are many other things one can do through this function only to change the returned object completely. In this post, we will see the use of the na_values parameter.
na_values: This is used to create a string that considers pandas as NaN (Not a Number). by-default pandas consider #N/A, -NaN, -n/a, N/A, NULL etc as NaN value. let’s see the example for better understanding.
so this is our dataframe it has three column names, class, and total marks. now import the dataframe in python pandas.
Example 1: see pandas consider #N/A as NaN.
Python3
import pandas as pd
df = pd.read_csv( 'Example.csv' )
print (df)
|
Output:
Example 2: Now the na_values parameter is used to tell pandas they consider “not available” as NaN value and print NaN at the place of “not available”.
Python3
import pandas as pd
df = pd.read_csv( 'Example.csv' ,
na_values = "not available" )
print (df)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...