将Excel行保存为Python中的列表

huangapple go评论74阅读模式
英文:

Saving Excel rows into lists in Python

问题

我基本上想要将每一行的前5个单元格保存为浮点数到一个列表(称之为list1),然后将第六个单元格保存到另一个列表(list2),然后将这两个列表保存到一个列表(list3)。我想对每一行都这样做,并将每一行的list3保存到一个最终列表中(对不起,我知道我说了很多次“列表”)。

所以例如,对于照片中的数据(标有Excel数据),我想保存如下:

final_list = [[[1.1,1.2,1.3,1.4,1.5],[1.6]], [[2.1,2.2,2.3,2.4,2.5],[2.6]],[[3.1,3.2,3.3,3.4,3.5],[3.6]]]

如果这有意义的话?如何以最简单的方式实现这一点,我是Python的初学者。

英文:

I basically want to save (for each row) the first 5 cells as floats to a list (say list1) then the sixth cell to another list (list2), then save both these to a list (list3). I want to do this for each rows and save each list3 for each row to one final list (sorry I know I said list alot),

So for example, for the data in the photo (labelled excel data below), I want to save it as:

final_list = [[[1.1,1.2,1.3,1.4,1.5],[1.6]], [[2.1,2.2,2.3,2.4,2.5],[2.6]],[[3.1,3.2,3.3,3.4,3.5],[3.6]]]

if that makes sense? What would be the easiest way to do this, I am a beginner at python.

excel data

答案1

得分: 0

Python 中处理表格的最常见方法是 Pandas。如果你只想要普通的列表,这个回答 使用了 openpyxl,可能会告诉你需要的内容。

对于你的情况,代码如下:

from openpyxl import load_workbook

path_to_wb = ...
my_wb = load_workbook(path_to_wb)

first_sheet = my_wb.worksheets[0]

whole_list = []
# 遍历所有行
for i in range(5):
    # 获取第一列数据
    first_columns = [first_sheet.cell(row=i + 1, column=j + 1).value
                     for j in range(5)]
    
    # 创建一个包含一个元素的列表,最后一列的数据
    last_element = [first_sheet.cell(row=i + 1, column=6).value]

    # 添加一个包含两个元素的列表
    whole_list.append([first_columns, last_element])

如果你喜欢列表推导式,可以摆脱循环:

whole_list = [[[first_sheet.cell(row=i + 1, column=j + 1).value for j in range(5)],
               [first_sheet.cell(row=i + 1, column=6).value]]
              for i in range(5)]

我要指出的是,这是你要求的方式,但这不是在Python中处理表格的常规方法。

英文:

Most common way of working with tables in Python is Pandas. If you just want plain lists this answer that uses openpyxl might tell you what you need.

For your case would be something like this:

from openpyxl import load_workbook

path_to_wb = ...
my_wb = load_workbook(path_to_wb)

first_sheet = my_wb.worksheets[0]

whole_list = []
# Iter all the rows
for i in range(5):
    # Get first columns
    first_columns = [first_sheet.cell(row = i + 1, column = j + 1).value
                     for j in range(5)]
    
    # Create a list with one single element, last column
    last_element = [first_sheet.cell(row = i + 1, column = 6).value]

    # Add a list with two elements
    whole_list.append([first_columns, last_element])

If you would prefer a list comprehension, you can get rid of the loop:

whole_list = [[[first_sheet.cell(row = i + 1, column = j + 1).value for j in range(5)],
               [first_sheet.cell(row = i + 1, column = 6).value]]
              for i in range(5)]

Let me point out that this is what you asked for, but it is not the usual methodology for working with tables in Python.

答案2

得分: -1

我认为这是你想要的:

假设数据是一个二维列表,其中每一行包含6个元素

data = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
        [7.0, 8.0, 9.0, 10.0, 11.0, 12.0],
        [13.0, 14.0, 15.0, 16.0, 17.0, 18.0]]

初始化一个空列表来存储最终结果

final_list = []

for row in data:
    # 提取行的前5个元素并转换为浮点数
    list1 = list(map(float, row[:5]))
    
    # 提取行的第6个元素并追加到list2中
    list2 = [row[5]]
    
    # 将list1和list2组合成一个列表
    list3 = list1 + list2  # 可能是 [list1, list2]
    
    # 将list3追加到最终列表中
    final_list.append(list3)
英文:

I think this is what you want:

Assuming data is a 2D list where each row contains 6 elements

data = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
        [7.0, 8.0, 9.0, 10.0, 11.0, 12.0],
        [13.0, 14.0, 15.0, 16.0, 17.0, 18.0]]

Initialize empty list to hold final results

final_list = []

for row in data:
    # Extract the first 5 elements of the row and convert to floats
    list1 = list(map(float, row[:5]))
    
    # Extract the 6th element of the row and append to list2
    list2 = [row[5]]
    
    # Combine list1 and list2 into a single list
    list3 = list1 + list2  # might be [list1, list2]
    
    # Append list3 to the final list
    final_list.append(list3)

huangapple
  • 本文由 发表于 2023年3月7日 22:07:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定