Sometimes a CSV file does not contain any DataFrame header. In such cases, the pandas Python library treats the first row of the DataFrame as a header. To address the problem, we can specify a custom header in the read_csv() function while creating a DataFrame.
For example, let’s say we are reading a CSV file iris2.csv and creating a DataFrame df.
import pandas df = pandas.read_csv("iris2.csv") print(df.head())
When we print the first few rows of the DataFrame, it shows the following:
5.1 3.5 1.4 0.2 setosa 0 4.9 3.0 1.4 0.2 setosa 1 4.7 3.2 1.3 0.2 setosa 2 4.6 3.1 1.5 0.2 setosa 3 5.0 3.6 1.4 0.2 setosa 4 5.4 3.9 1.7 0.4 setosa
As we can see the DataFrame header is missing in the DataFrame. And pandas treats the first row of the DataFrame as the header.
So, we can provide the name of the columns as a header while reading the CSV file.
import pandas df = pandas.read_csv("iris2.csv", names=["Sepal Length", "Sepal Width", "Petal Length", "Petal Width", "Species"]) print(df.head())
The five columns of the DataFrame are now named “Sepal Length”, “Sepal Width”, “Petal Length”, “Petal Width”, and “Species”, respectively. So, at this point, if we print the first few lines of the DataFrame, the output will be:
Sepal Length Sepal Width Petal Length Petal Width Species 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa
As we can see the DataFrame header is successfully added while reading the CSV file using the read_csv() function.






0 Comments