Python将列表分为给定长度的子列表

时间:2020-09-18 16:06:05

标签: pandas nested-lists

我目前正试图从一个单列pandas数据框中提取数据到8个包含12个浮点值的列表中,以制作一个热图(12 x 8矩阵中的96个数据点)。

我的元素在列表中的列表中(从csv文件中提取,此处显示其外观):

my_list = data['Abs 590 nm'].values.tolist() 
list_for_plotting_routine = [my_list]

#This is what the list looks like when printed
list_for_plotting_routine = [[0.0, -0.006, 0.01, -0.004, 0.0, -0.002, -0.002, 0.017, 0.0, 0.017, 0.003, -0.001, -0.003, -0.005, 0.012, 0.031, -0.004, -0.004, -0.004, 0.004, 0.002, 0.003, 0.004, 0.001, -0.003, 0.009000000000000001, -0.006, 0.011000000000000001, 0.011000000000000001, 0.032, 0.013999999999999999, 0.069, 0.004, 0.011000000000000001, 0.01, 0.012, -0.004, -0.006, -0.006999999999999999, 0.004, 0.01, 0.011000000000000001, 0.016, 0.043, 0.023, 0.016, 0.025, 0.011000000000000001, 0.003, -0.004, -0.003, 0.005, 0.017, 0.017, 0.011000000000000001, 0.052000000000000005, 0.033, 0.017, 0.01, 0.012, -0.008, -0.005, -0.006999999999999999, -0.005, -0.006, 0.0, 0.006999999999999999, 0.006999999999999999, 0.001, -0.005, 0.017, 0.009000000000000001, 0.005, 0.001, -0.001, 0.003, -0.002, 0.024, 0.02, 0.017, 0.011000000000000001, 0.01, 0.005, -0.006, 0.01, 0.013999999999999999, 0.01, 0.013999999999999999, 0.005, 0.008, 0.002, 0.006999999999999999, 0.002, -0.002, 0.024, 0.006]]```

#Now, I would like to have the following format, which works with the heatmap (https://plotly.com/python/heatmaps/ )

#data_desired = [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3], 
 #              [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3],
  #             [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3],
   #            [0.1, 0.2, 0.3, 0.8, 0.5, 0.7, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3],
    #           [0.1, 0.2, 0.1, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3],
     #          [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3],
      #         [0.1, 0.2, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3],
       #        [0.1, 0.2, 0.5, 0.4, 0.5, 0.6, 0.4, 0.8, 0.9, 1.1, 1.2, 1.4]]```
 
#I have tried 3 different solutions, from https://www.geeksforgeeks.org/python-split-a-list-into-#sublists-of-given-lengths/ and  https://stackoverflow.com/questions/55294735/python-how-to-splice-a-#list-into-sublists-of-given-lengths

  
lengths = [12,12,12,12,12,12,12,12]

res = []
start_index = 0
for length in lengths:
    res.append(list_for_plotting_routine[start_index:start_index+length])
    start_index += length

print(res)

#and using 
from itertools import accumulate 
from itertools import islice 
  
# list of length in which we have to split 
length_to_split = [12, 12, 12, 12, 12, 12, 12, 12] 
  

    # Using islice 
Output = [list_for_plotting_routine[x - y: x] for x, y in zip( 
        accumulate(length_to_split), length_to_split)] 
  
# Printing Output 
print("Initial list is:", list_for_plotting_routine) 
print("Split length list: ", length_to_split) 
print("List after splitting", Output) 

 
# Using islice 
Output = [Input[x - y: x] for x, y in zip( 
         accumulate(length_to_split), length_to_split)] 
  
## Printing Output 
print("Initial list is:", Input) 
print("Split length list: ", length_to_split) 
print("List after splitting", Output)

#All solutions results in the same output:

[[[0.0, -0.006, 0.01, -0.004, 0.0, -0.002, -0.002, 0.017, 0.0, 0.017, 0.003, -0.001, -0.003, -0.005, 0.012, 0.031, -0.004, -0.004, -0.004, 0.004, 0.002, 0.003, 0.004, 0.001, -0.003, 0.009000000000000001, -0.006, 0.011000000000000001, 0.011000000000000001, 0.032, 0.013999999999999999, 0.069, 0.004, 0.011000000000000001, 0.01, 0.012, -0.004, -0.006, -0.006999999999999999, 0.004, 0.01, 0.011000000000000001, 0.016, 0.043, 0.023, 0.016, 0.025, 0.011000000000000001, 0.003, -0.004, -0.003, 0.005, 0.017, 0.017, 0.011000000000000001, 0.052000000000000005, 0.033, 0.017, 0.01, 0.012, -0.008, -0.005, -0.006999999999999999, -0.005, -0.006, 0.0, 0.006999999999999999, 0.006999999999999999, 0.001, -0.005, 0.017, 0.009000000000000001, 0.005, 0.001, -0.001, 0.003, -0.002, 0.024, 0.02, 0.017, 0.011000000000000001, 0.01, 0.005, -0.006, 0.01, 0.013999999999999999, 0.01, 0.013999999999999999, 0.005, 0.008, 0.002, 0.006999999999999999, 0.002, -0.002, 0.024, 0.006]], [], [], [], [], [], [], []]

为什么我得到的空列表而不是包含12个浮点值的8个列表...?以及如何将数据转换为所需的格式以生成热图?

2 个答案:

答案 0 :(得分:2)

这是一种使用数据绘制热图的简单解决方案。无需将数组拆分为循环。

df = data['Abs 590 nm'].values.reshape(8,12)
ax = sns.heatmap(df)

seaborn heatmap

答案 1 :(得分:1)

import pandas as pd
import seaborn as sns

list_for_plotting_routine = [[0.0, -0.006, 0.01, -0.004, 0.0, -0.002, -0.002, 0.017, 0.0, 0.017, 0.003, -0.001, -0.003, -0.005, 0.012, 0.031, -0.004, -0.004, -0.004, 0.004, 0.002, 0.003, 0.004, 0.001, -0.003, 0.009000000000000001, -0.006, 0.011000000000000001, 0.011000000000000001, 0.032, 0.013999999999999999, 0.069, 0.004, 0.011000000000000001, 0.01, 0.012, -0.004, -0.006, -0.006999999999999999, 0.004, 0.01, 0.011000000000000001, 0.016, 0.043, 0.023, 0.016, 0.025, 0.011000000000000001, 0.003, -0.004, -0.003, 0.005, 0.017, 0.017, 0.011000000000000001, 0.052000000000000005, 0.033, 0.017, 0.01, 0.012, -0.008, -0.005, -0.006999999999999999, -0.005, -0.006, 0.0, 0.006999999999999999, 0.006999999999999999, 0.001, -0.005, 0.017, 0.009000000000000001, 0.005, 0.001, -0.001, 0.003, -0.002, 0.024, 0.02, 0.017, 0.011000000000000001, 0.01, 0.005, -0.006, 0.01, 0.013999999999999999, 0.01, 0.013999999999999999, 0.005, 0.008, 0.002, 0.006999999999999999, 0.002, -0.002, 0.024, 0.006]]

# Flatten list of lists
list_for_plotting_routine = [item for sublist in list_for_plotting_routine for item in sublist]

# Split list into seperate lists for every 12th element
list_of_lists = [list_for_plotting_routine[x:x+12] for x in range(0, len(list_for_plotting_routine), 12)]

# Create a df out of list of lists (this step is optional you can just pass in the list of lists instead to sns.heatmap()

df = pd.DataFrame(list_of_lists)

# create heatmap
ax = sns.heatmap(df)
相关问题