Predictive Hacks

How to Read and Write CSV files without Headers with Pandas

Read Without Headers

Assume that you have the following CSV file which is without column names. Let’s see how we can read it with pandas.

my_file.csv

George,25
Maria,30
John,18

We can read the file as follows:

import pandas as pd
df = pd.read_csv("my_file.csv", header=None)
df

In case we want to give specific names to our column names:

df1 = pd.read_csv("my_file.csv", header=None, names=['colA', 'colB'])
df1 

Write Without Headers

Now, let’s say that we want to write this dataframe to a csv file but without headers.

df.to_csv('filename.csv', header=False, index=False)

As we can see the filname.csv is without headers!

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s