简体   繁体   English

我想将特定列保存到 python 中的新 excel 表中

[英]i want to save a particular column into a new excel sheet in python

if this is the spread sheet then I want to make a new spreadsheet containing only product id :如果这是电子表格,那么我想制作一个仅包含product id的新电子表格:

transaction id product id
1001           1    
1002           2
1003           3

the code I have written so far I am able to print a particular column到目前为止我编写的代码我能够打印一个特定的列

import openpyxl as xl
from openpyxl import Workbook
wb = xl.load_workbook('transactions.xlsx')
sheet = wb['Sheet1']
book = Workbook()
x = input("row name")
y=int(x)
book.save("sample.xlsx")
for row in sheet.rows:
     print(row[y].value)

Why not just use to_excel .为什么不直接使用to_excel Its a lot less complicated:它不那么复杂:

df[['product_id']].to_excel('sample.xlsx', index = False)  #Keeping index = True will have an extra row with index

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-使用现有格式保存新的Excel工作表 - Python - save new Excel sheet with existing format Python Pandas:在Excel中获取特定行并保存到新表中 - Python pandas: Get specific row in an excel and save to a new sheet 我是python的新手,我想使用python计算excel中列的字符串 - I am new to python and i want to calculate the string of the column in excel using python 有没有办法将特定列从 Excel 工作表(例如 sheet_1)复制到 sheet_2 中的另一列? 使用 Python - Is there any way to copy a particular column from excel sheet (say sheet_1) to the other column which is in sheet_2? Using Python 如何在Excel中将Excel Sheet保存为HTML? - How do I save Excel Sheet as HTML in Python? 如果我想在特定列上按总和聚合,如何在 python 中应用“groupby”? - How to apply 'groupby' in python if I want to aggregate by sum on a particular column? 我想在 pandas 中使用.to_excel 创建的 excel 中添加一个新工作表 - I want to add a new sheet in an excel created using .to_excel in pandas 如何使用python动态创建新的Excel工作表? - How do I create a new excel sheet dynamically using python? 我想相对于列对表格中 excel 文件中存在的数据进行排序。 (excel文件有多张) - I want to sort data present in excel file in sheet with respect to column. (The excel file has multiple sheets) 想要 python 中的活动 excel 工作表名称? - Want a active excel sheet names in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM