Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write excel row in python using pandas

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()
like image 658
Tim Avatar asked Nov 19 '25 02:11

Tim


1 Answers

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]]
like image 130
jezrael Avatar answered Nov 21 '25 15:11

jezrael