Search code examples
pythonpandascsvglob

How to merge data frame into one csv file after using glob?


I have tried to work on several csv files using glob, for example:

import glob 
import pandas as pd
import numpy as np
import csv

# Read all csv files with same file name in the folder
filenames = sorted(glob.glob('./16_2018-02*.csv'))

for f in filenames:
    df = pd.read_csv(f, names=['Date','RSSI','Data','Code'], 
    index_col=None)

    # Slicing information 
    df["ID"] = df["Data"].str.slice(0,2)
    df["X"] = df["Data"].str.slice(2,4)

    # Save the output data to csv with different name 
    df.to_csv(f'{f[:-4]}-train.csv', index=False)

In the end of the code, I used to save each dataframe into a new csv file with different name. Considering now I have so many csv data to work with, I want to concatenate them without first writing into each csv file. How should I do that?

Original dataset first 5 rows:

Date                            RSSI    Data                        Code        
2018-02-20T00:00:20.886+09:00   -99 1068ffd703d101ec77f425ea98b201  F2D5    
2018-02-20T00:00:21.904+09:00   -95 103cffbc032901ee77f49dea98b301  F2D5        
2018-02-20T00:00:22.415+09:00   -97 103cffbc032901ee77f49dea98b301  F2D5         
2018-02-20T00:00:46.580+09:00   -96 10fdfda803ff01f477f49dfd98cb03  F2D1        
2018-02-20T00:00:48.593+09:00   -96 101bfed3037401f577f49dfe98cd03  F2D6    

After:

Date                            RSSI    Data                        Code    ID  X   
2018-02-20T00:00:20.886+09:00   -99 1068ffd703d101ec77f425ea98b201  F2D5    16  0.065384    
2018-02-20T00:00:21.904+09:00   -95 103cffbc032901ee77f49dea98b301  F2D5    16  0.065340        
2018-02-20T00:00:22.415+09:00   -97 103cffbc032901ee77f49dea98b301  F2D5    16  0.065340         
2018-02-20T00:00:46.580+09:00   -96 10fdfda803ff01f477f49dfd98cb03  F2D1    16  0.065021    
2018-02-20T00:00:48.593+09:00   -96 101bfed3037401f577f49dfe98cd03  F2D6    16  0.065051    

Solution

  • Try the below code [for appending all the files to 1 file]:

    filenames = sorted(glob.glob('./16_2018-02*.csv'))
    appended_data=[] #create a list
    for f in filenames:
        df = pd.read_csv(f, names=['Date','RSSI','Data','Code'], 
        index_col=None)
    
        # Slicing information 
        df["ID"] = df["Data"].str.slice(0,2)
        df["X"] = df["Data"].str.slice(2,4)
        appended_data.append(df) #append to the list
    appended_data = pd.concat(appended_data, axis=1) #concat them together
    #remove axis=1 if need to append vertically
    

    The appended_data is now a dataframe with all files appended together post which you can export the same to csv/excel.