【问题标题】:How to Read Text file as input and write into excel columns using panda?如何使用熊猫读取文本文件作为输入并写入excel列?
【发布时间】:2017-11-30 09:29:43
【问题描述】:

我使用permission.txt 文件作为输入,并希望将数据写入excel -2007 的列(我使用panda XlsxWriter 因为我想要超过256 列)。 I want to write like this into excel file。我已经尝试了以下代码,它将数据写入行而不是我想将数据写入列(第 1 列,第 2 列......第 400 列)。

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy as np


data = pd.read_csv('F:/Research_Work/python/Permission.txt', sep=" ", header=None)
writer = ExcelWriter('Example2.xlsx')
data.to_excel(writer,'Sheet1',index=False)

【问题讨论】:

    标签: python python-3.x python-2.7 pandas xlsxwriter


    【解决方案1】:

    您可以像这样转置数据帧数据:

    import pandas as pd
    
    # Create a Pandas dataframe from some data.
    df1 = pd.DataFrame({'Data': [10, 20, 30, 40]})
    df2 = df1.T
    
    # Create a Pandas Excel writer using XlsxWriter as the engine.
    writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
    
    # Write the data in column and transposed (row) directions.
    df1.to_excel(writer, sheet_name='Sheet1', 
                 startrow=1, startcol=1, header=False, index=False)
    
    df2.to_excel(writer, sheet_name='Sheet1', 
                 startrow=1, startcol=3, header=False, index=False)
    
    # Close the Pandas Excel writer and output the Excel file.
    writer.save()
    

    输出:

    【讨论】:

    • 感谢这项工作。我添加了以下内容以读取文本文件。 df1 = pd.read_table('F:/Research_Work/python/Permission.txt', delim_whitespace=True) 。请添加您的答案。
    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2022-11-30
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多