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)