3

I am reading a row from an excel file and trying to write it back to excel as row but it writes as column, I am using pandas

import pandas as pd 
import xlsxwriter as xlsw

cols = [1, 2, 3, 4, 5, 6, 7]
df = pd.read_excel('test.xlsx', sheet_names='Sheet1', usecols=cols)
df.head()
dfr = df.iloc[6]
dfr = pd.DataFrame(dfr,columns=['Voltage', 'Amps', 'Expected 
Voltage','Expected Current', 'ExpectedLogicalValue', 'Actual Voltage'])

writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
dfr.to_excel(writer, sheet_name="data", index=False)
writer.save()
1

1 Answer 1

3

I think you need select by double list for one row DataFrame and then set new columns names:

dfr = df.iloc[[6]]
dfr.columns= = ['Voltage', 'Amps', 'Expected Voltage','Expected Current', 
                'ExpectedLogicalValue', 'Actual Voltage', 'Another col']

Another solution:

cols = [1, 2, 3, 4, 5, 6, 7]
names = ['Voltage', 'Amps', 'Expected Voltage','Expected Current', 
         'ExpectedLogicalValue', 'Actual Voltage', 'Another col']

df = pd.read_excel('test.xlsx', sheet_names='Sheet1', usecols=cols, names=names, skiprows=1)

dfr = df.iloc[[6]]
Sign up to request clarification or add additional context in comments.

1 Comment

I edited this line of my code and everything worked dfr = df.iloc[[6]]

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.