简体   繁体   English

如何使用熊猫在python中编写Excel行

[英]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.

相关问题 如何在Python中使用Pandas将数据数组写为Excel而不是按行 - How to write an data array to excel in a row instead of in a column using pandas in Python 使用 Pandas 将 Dataframe 行写入 excel 表 - Write Dataframe row to excel sheet using Pandas 如何使用熊猫python在excel中为最后一行设置颜色 - How to set color for last row in excel using pandas python 使用python pandas在excel中添加空行 - Add empty row in excel using python pandas 如何将 Pandas Dataframe 从 python 水平写入 Excel 表中,连续列打开 - How to write Pandas Dataframe Horizontally in to Excel sheet from python openpyxl (elements in same row, consecutive columns) Pandas & Python:如何写入特定的 excel 行,而不替换其原始值 - Pandas & Python: how to write to a specific excel row, without replacing its original value 如何使用 Pandas 将数据写入带有标题的excel? - How to write data to excel with header using Pandas? 熊猫-如何使同一行空白然后使用合并的单元格写入excel - pandas - how to make same row blank then write to excel with merged cells Python-Excel:如何将一行行写入到现有的excel文件中? - Python-Excel: How to write lines of a row to an existing excel file? 如何使用启用了 skip_blanks 的 Python xlwings 将 pandas df 写入 Excel? - How to write a pandas df into Excel using Python xlwings with skip_blanks enabled?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM