I am new to Python, and not much of a coder. I have 40+ text files that I want to combine together (in a 'wide' csv, as opposed to a 'tall' csv. That is, I don't want to append the files) and produce a new csv.
Using Pandas (merge) I am able to achieve what I want, but I presume there is a simpler way. Here it is on seven of the files:
import pandas as pd
a = pd.read_csv("c:/pyTest/B01001.txt")
b = pd.read_csv("c:/pyTest/B01002.txt")
c = pd.read_csv("c:/pyTest/B01003.txt")
d = pd.read_csv("c:/pyTest/B02001.txt")
e = pd.read_csv("c:/pyTest/B05001.txt")
f = pd.read_csv("c:/pyTest/B05002.txt")
g = pd.read_csv("c:/pyTest/B05012.txt")
merged = a.merge(b.merge(c.merge(d.merge(e.merge(f.merge(g, on='GEOID'), on='GEOID'), on='GEOID'), on='GEOID'), on='GEOID'), on='GEOID')
merged.to_csv("c:/pytest/fook.csv", index=False)
It would be great if the duplicated column names (eg 'GEOID') weren't repeated in the output file too.
Any help from you experts greatly appreciated.