1

I am new in python and pandas. I have to read few csv files which have same columns and created a resultant dataFrame(which have all the rows from every csv files). I tried it but when i print dataframe, it's print Empty DataFrame

Columns: [] Index: []

code is:

def readCSV(dir):
list = getFilesInDir(dir) #  my function which returns list of files.
dataframe = pandas.DataFrame()    
for name in list:
    df = pandas.read_csv(name)
    dataframe.append(df)

print(dataframe)
1

1 Answer 1

3

DataFrame.append is not list.append. You need to assign the result back.

dataframe = dataframe.append(df)

However, appending within a loop is not advised as it needlessly copies data. You should append to a list and concatenate once in the end. We can turn the loop into a list comprehension within concat.

import pandas as pd

dataframe = pd.concat([pd.read_csv(name) for name in list])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.