简体   繁体   中英

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 :

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 . Its a lot less complicated:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM