How to separate extracted column from excel into different lists or groups in Python

我是研究僧i 提交于 2019-12-02 16:13:18

问题


I am reading a column from an excel file using openpyxl.

I have written code to get the column of data I need into excel but the data is separated by empty cells.

I want to group these data wherever the cell value is not None into 19 sets of countries so that I can use it later to calculate the mean and standard deviation for the 19 countries.

I don't want to hard code it using list slices. Instead I want to save these integers to a list or list of lists using a loop but im not sure how to because this is my first project with Python.

Here's my code:

    #Read PCT rankings project ratified results
#Beta

import openpyxl

wb=openpyxl.load_workbook('PCT rankings project ratified results.xlsx', data_only=True)
sheet=wb.get_sheet_by_name('PCT by IP firms')

row_counter=sheet.max_row
column_counter=sheet.max_column

print(row_counter)
print(column_counter)

#iterating over column of patent filings and trying to use empty cells to flag loop for it to append/store list of numbers before reaching the next non empty cell and repeat this everytime it happens(expecting 19 times) 

list=[]
for row in range(4,sheet.max_row +1):
    patent=sheet['I'+str(row)].value
    print(patent)
    if patent == None:
        list.append(patent)
print(list)

This is the output from Python giving you a visualisation of what I am trying to do.

Column I: 412 14 493 488 339 273 238 226 200 194 153 164 151 126 None 120 None None 133 77 62 79 24 0 30 20 16 0 6 9 11 None None None None 608 529 435 320 266 264 200 272 134 113 73 23 12 52 21


回答1:


You should use csv library ! 😉 csv means comma separator value ! Excel file can be translate in this type of file . And csv file are relatively easy to use. You have to look for this way. Ask me if you have more questions .. Hopefully I helped you ! Talk you soon



来源:https://stackoverflow.com/questions/47443839/how-to-separate-extracted-column-from-excel-into-different-lists-or-groups-in-py

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