How to read csv file with Pandas without header¶
Introduction:¶
Dealing with CSV files that lack column headers is a common scenario in data analysis. In this tutorial, we’ll explore how to read and work with such files using Python and the pandas library. By the end of this guide, you’ll be equipped with the skills to handle CSV files without predefined column names.
Prerequisites:¶
Before we start, ensure that you have Python installed on your machine. Additionally, install the pandas library using the following command:
In [11]:
#pip install pandas
Step 1: Importing Necessary Libraries¶
In [12]:
import pandas as pd
Step 2: Reading the CSV File with No Column Headers¶
In [13]:
# Replace 'your_file.csv' with the actual file path
df = pd.read_csv('google_map_business_data.csv', header=None)
Explanation:¶
We use the read_csv function from pandas to read the CSV file.
The header=None argument specifies that the CSV file doesn’t contain a header row.
Step 3: Adding Custom Column Names¶
In [14]:
# Provide custom column names
column_names = ['company_name', 'rating', 'reviews_count', 'address', 'category', 'phone', 'website']
df.columns = column_names
SIMPLE WAY¶
In [15]:
df = pd.read_csv('google_map_business_data.csv', header=None, names=['company_name', 'rating', 'reviews_count', 'address', 'category', 'phone', 'website'])
In [ ]:
In [ ]: