简体   繁体   中英

Combining CSV files into Dataframe python

I am trying to add data from several files in a folder to a data frame. Each.csv file has varying lengths but has the same number of columns. I am trying to add all of them to one data frame with ignoring the index so that the new data frame is just vertically combined. For some reason every time I try to concatenate the data I am left with ~ 363 columns when there should only be 9. Each csv file has the same number of columns so I am confused.

import os 
import pandas as pd 
import glob 

cwd = os.getcwd()
folder = cwd +'\\downloads\\prepared_csv_files\\prepared_csv_files\\' 
all_files = glob.glob(folder + "/*.csv") 
    
li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

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

I have also tried

final_df = pd.DataFrame(li, columns = ['tool','pressure'])
# and I name all columns not doing it now 

here final is the name of the final dataset.

I am assuming tool and pressure are the columns name in your all.csv files

final = pd.DataFrame(columns = ['tool','pressure'])
    for filename in all_files:
          df = pd.read_csv(filename)
          df = pd.DataFrame(df)
          final = pd.concat([final,df],ignore_index= True,join="inner")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM