简体   繁体   English

如何将多个 python 数据帧转储/写入一个文本文件并保留它们的标题行?

[英]How to dump/write multiple python dataframes to a text file keeping their header lines?

I have multiple pandas dataframes each with a different shape.我有多个熊猫数据框,每个数据框都有不同的形状。 Some dataframes have 2 lines of header lines, some have only 1 header line.有些数据帧有 2 行标题行,有些只有 1 行标题行。 Each a different number of columns.每个不同的列数。

How to dump/write multiple python dataframes to a text file keeping their header lines?如何将多个 python 数据帧转储/写入一个文本文件并保留它们的标题行?

I just tried this and it worked :我刚试过这个,它奏效了:

df1.to_csv(r"C:\Users\sb512911\Desktop\All\Applications\VDR\output\out.txt", sep = ' ', mode = 'a')

df2.to_csv(r"C:\Users\sb512911\Desktop\All\Applications\VDR\output\out.txt", sep = ' ', mode = 'a')

Please see if this works for you as well.请看看这是否也适用于您。

Thanks, I did in the old manner.谢谢,我用旧的方式。 Without thinking in dataframes, I read file and write line by line.没有考虑数据帧,我逐行读取文件并写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM