3

I have multiple text files in a folder than I'm outputting to make a table. I got the table to export to a .csv file successfully. The problem is I want to add a header to each of the columns and I want the names of the headers to be the text file's name. Each file is a column of data in the table. So for example, column 1 is from textfile.1. I want to add a header to column 1 that says "textfile.1"

Here is my working code:

import os

path = r'C:/path/to/file'
folders = os.listdir(path) #raw string

import pandas as pd

df = pd.DataFrame()
df_interim = pd.DataFrame()

for f in folders:
    df_interim = pd.read_csv(
        os.path.join(path,f ,),
        header=None
    )
    #concatenate the data into the original dataframe
    frames = [df, df_interim]
    df = pd.concat(frames, axis=1)
df.to_csv('outputfile',index=False)
Red
  • 26,798
  • 7
  • 36
  • 58

1 Answers1

2

Fixing your code, you just need to add the names param and set it to your filename:

df_interim = pd.read_csv(os.path.join(path, f),
                         header=None,
                         names=[f])

See here for more on arguments to read_csv.


Here's a simpler way to do it that removes the loop and df_interim;

df_final = pd.concat([
    pd.read_csv(os.path.join(path, f), header=None, names=[f], squeeze=True)
    for f in folders
  ], axis=1
)
cs95
  • 379,657
  • 97
  • 704
  • 746