Looping through an Excel spreadsheet (using openpyxl)

大兔子大兔子 提交于 2019-12-02 23:37:59

问题


import openpyxl wb=openpyxl.load_workbook('Book_1.xlsx') ws=wb['Sheet_1']

I am trying to analyze an excel spreadsheet using openpyxl. My goal is to get the max number from column D for each group of numbers in column A. I would like help in getting a code to loop for the analysis. Here is an example of the spreadsheet that I am trying to analyze. The file name is Book 1 and the sheet name is Sheet 1. I am running Python 3.6.1, pandas 0.20.1, and openpyxl 2.4.7. I am providing the code I have so far.


回答1:


IIUC, use pandas module to achieve this:

import pandas as pd
df = pd.read_excel('yourfile.xlsx')
maxdf = df.groupby('ID').max()

maxdf will have the result you are looking for.




回答2:


Let's say you have file test.xlsx with worksheet ws1. Try:

from openpyxl import load_workbook
wb = load_workbook(filename='test.xlsx')
ws = wb['ws1']

for col in ws.columns:
    col_max = 0
    for cell in col:
        if cell.value > col_max:
            col_max = cell.value
    print('next max:', col_max)

I'm looping over all the rows because I'm not sure what you've expected.



来源:https://stackoverflow.com/questions/44530201/looping-through-an-excel-spreadsheet-using-openpyxl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!