0

I am working on a project in which I want to load a dataframe using the csv file and check if the file from which I want to load the dataframe is empty or not. If the csv file is empty then as soon as the statement df=pd.read_csv(file.csv) is encountered , I get the error pandas.errors.EmptyDataError: No columns to parse from file Please help me

#custom error class defined correctly
try:
    #file.csv is an empty csv file
   df=pd.read_csv(file.csv)
   if df:
        print("Dataframe loaded successfully!!")
   else:
        raise Empty_csv_file_Error("The csv file is empty!!")
except Empty_csv_file_Error as e:
    print(e.msg)

Error encountered while loading the dataframe using empty csv file :- pandas.errors.EmptyDataError: No columns to parse from file

1 Answer 1

2

The pandas error is telling you that the file is empty, so just catch it:

import pandas as pd

try:
    #file.csv is an empty csv file
   df=pd.read_csv("file.csv")
except pd.errors.EmptyDataError:
    print("The CSV file is empty")
else:
    print("Dataframe loaded successfully!!")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.