[英]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 我正在从excel文件中读取一行,并尝试将其写回excel,但它写为列,但我使用的是熊猫
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()
I think you need select by double list
for one row DataFrame and then set new columns names: 我认为您需要按一列DataFrame的双重
list
进行选择,然后设置新的列名称:
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.