0

I am using permission.txt file as intput and want to write data into columns of excel -2007(I am using panda XlsxWriter becasue I want more than 256 columns). I want to write like this into excel file. I have tried following code up to this which writes data in rows instead I want to write data into columns(column 1,column 2......column 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)

1 Answer 1

1

You could just transpose the dataframe data, like this:

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()

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this work for me. I have added followingto read text file. df1 = pd.read_table('F:/Research_Work/python/Permission.txt', delim_whitespace=True) . Please add in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.