如何将多个csv组合为python中的列?



我有10个.txt (csv)文件,我想将它们合并在一个csv文件中,以便稍后在分析中使用。当我使用时,pd.append

我使用以下代码:

master_df = pd.DataFrame()
for file in os.listdir(os.getcwd()):
if file.endswith('.txt'):
files = pd.read_csv(file, sep='t', skiprows=[1])
master_df = master_df.append(files)

输出为:

输出我需要的是插入每个文件的列并排,如下所示:

所需输出

你能帮我一下吗?

要合并并排的dataframe,您应该使用pd.concat.

frames = []
for file in os.listdir(os.getcwd()):
if file.endswith('.txt'):
files = pd.read_csv(file, sep='t', skiprows=[1])
frames.append(files)
# axis = 0 has the same behavior of your original approach
master_df = pd.concat(frames, axis = 1)

最新更新