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)
CrazyC
  • 1,840
  • 6
  • 39
  • 60
  • This is literally https://stackoverflow.com/questions/13784192/creating-an-empty-pandas-dataframe-then-filling-it, please don't fill an empty DataFrame, it is a bad practice in pandas – cs95 Mar 22 '20 at 23:34

1 Answers1

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])
ALollz
  • 57,915
  • 7
  • 66
  • 89