Search code examples
pythonpandasdataframeglob

How to create a single dataframe from many dataframes in python?


I want to make a dataframe from multiple files but It doesn't work the way I want. I want to merge vertically but they are merging horizontally creating new columns. I don' want that how can I fix this? Here is my code :

path = "C:/Users/nb/Desktop/NetworkCharacteristic/"
fi=glob.glob(path+"*")
li=[]
for file in fi:
    files = glob.glob(file + "/*.txt")
for filename in files:
    frame = pd.read_csv(filename, sep='#')
    li.append(frame)
df = pd.concat(li, axis=1)

i read files from many folders inside a folder. And I get that result : "5 rows × 4440 columns"


Solution

  • You can replace this line of code :

    df = pd.concat(li, axis=1)
    

    By :

    df = pd.concat(li, axis=0, ignore_index=True)
    

    However, if you want a most straitforward way to do it, I suggest :

    import glob
    import pandas as pd
    
    files = glob.glob("C:/Users/nb/Desktop/NetworkCharacteristic/*/*.txt")
    dfs = [pd.read_csv(f, header=None, sep="#") for f in files]
    
    df = pd.concat(dfs, ignore_index=True)