Search code examples
csvpandasmergeglob

Horizontal append in for loop?


I have a for loop iterating over a folder of one column csv's using glob, it makes some adjustments and then appends the results to a list and saves to a new csv, it resembles:

data= []
infiles = glob.glob("*.csv")
for file in infiles:
    df = pd.io.parsers.read_csv(file)
    (assorted adjustments)
    data.append(df)
fullpanel = pd.concat(panel)
fullpanel.to_csv('data.csv')  

The problem is that makes one long column, I need each column (of differing lengths) added next to each other.


Solution

  • I think you can add parameter axis=1 to concat for columns added next to each other. Also you can change pd.io.parsers.read_csv to pd.read_csv and panel to data in concat.

    data= []
    infiles = glob.glob("*.csv")
    for file in infiles:
        df = pd.read_csv(file)
        (assorted adjustments)
        data.append(df)
    fullpanel = pd.concat(data, axis=1)
    fullpanel.to_csv('data.csv')