
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to read a Pandas CSV file with no header?
To read a CSV file with no header, we can use headers in read_csv() method.
Steps
- Initialize a variable file_path, i,e., CSV file path.
- Use read_csv method to get the DataFrame with tab separator and with headers.
- Print the DataFrame with headers.
- Use read_csv method to get the DataFrame with tab separator and without headers. To read without headers, use header=0.
- Print the DataFrame without headers.
Example
import pandas as pd file_path = 'test.csv' // With Headers df = pd.read_csv(file_path, sep='\t', names=['x', 'y', 'z']) print "With headers, the DataFrame is: \n", df // Without Headers df = pd.read_csv(file_path, sep='\t', header=0, names=['x', 'y', 'z']) print "Without headers, the DataFrame is: \n", df
The CSV file "test.csv" contains the following data
x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0
Output
With headers, the DataFrame is: x y z NaN x y z 0.0 5 4 4 1.0 2 1 1 2.0 1 5 5 3.0 9 10 0 Without headers, the DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0
- Related Articles
- Python - Read csv file with Pandas without header?
- How to read CSV file in Python?
- Writing a Pandas DataFrame to CSV file
- How to read data from *.CSV file using JavaScript?
- How to read data from .csv file in Java?
- Python - How to write pandas dataframe to a CSV file
- How to read the data from a CSV file in Java?\n
- Python Pandas- Create multiple CSV files from existing CSV file
- Python - Read all CSV files in a folder in Pandas?
- How to read a CSV file and store the values into an array in C#?
- How to Handle Large CSV files with Pandas?
- How to read a HTTP header using JSP?
- How to read a text file with C++?
- How to read a JSON file into a DataFrame using Python Pandas library?
- How to read CSV files in Golang?

Advertisements