Python将2个列表和Pandas DataFrames依次写入csv/excel [英] Python write 2 lists and Pandas DataFrames to csv/excel sequentially
本文介绍了Python将2个列表和Pandas DataFrames依次写入csv/excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这些Python列表和Pandas数据框:
I have these Python lists and Pandas Dataframes:
list_1 = ['Intro line here - record of method function:']
list_2 = ['Record of local minimum follows:']
print df_1
Col_A Col_B
3.4443 1.443
10.8876 11.99
print df2
Trial_1 Trial_2 Trial_3
1.1 1.49 775.9
11.5 9.57 87.3
384.61 77.964 63.7
12.49 0.156 1.9
112.11 11.847 178.3
这是我想要的输出csv或excel文件中的内容-csv或excel都可以为我工作:
Here is what I want in the output csv or excel file - either csv or excel would work for me:
Intro line here - record of method function:
Col_A Col_B
3.4443 1.443
10.8876 11.99
Record of local minimum follows:
Trial_1 Trial_2 Trial_3
1.1 1.49 775.9
11.5 9.57 87.3
384.61 77.964 63.7
12.49 0.156 1.9
112.11 11.847 178.3
是否可以按此顺序将列表,熊猫,列表,熊猫写入csv或excel文件?
Is there a way to write the list, Pandas, list, Pandas in this order to the csv or excel file?
推荐答案
pd.to_csv()
接受文件句柄作为输入,而不仅仅是文件名.因此,您可以打开文件句柄并将多个文件写入其中.这是一个示例:
pd.to_csv()
accepts a file handle as input, not just a file name. So you can open a file handle and write multiple files into it. Here's an example:
from __future__ import print_function
with open('output.csv', 'w') as handle:
for line in list_1:
print(line, handle)
df1.to_csv(handle, index=False)
for line in list_2:
print(line, handle)
df2.to_csv(handle, index=False)
这篇关于Python将2个列表和Pandas DataFrames依次写入csv/excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文