如何将元素分组并在Python中将它们全部列出?
我有这样的想法:如何将元素分组并在Python中将它们全部列出?,python,arrays,pandas,numpy,Python,Arrays,Pandas,Numpy,我有这样的想法: time 0 1 2 3 4 5 Val1 32 12 56 45 3 67 Val2 60 34 2 5 13 90 dataset= [ [["32", "12", "56"], ["45", "3", "67"]], [["60", "34", "2"], ["5", "13", "90"]] ] 我想要一个由3个值组成的子数组列表,如下所示: time 0 1 2 3 4 5 Val1 32
time 0 1 2 3 4 5
Val1 32 12 56 45 3 67
Val2 60 34 2 5 13 90
dataset= [
[["32", "12", "56"], ["45", "3", "67"]],
[["60", "34", "2"], ["5", "13", "90"]]
]
我想要一个由3个值组成的子数组列表,如下所示:
time 0 1 2 3 4 5
Val1 32 12 56 45 3 67
Val2 60 34 2 5 13 90
dataset= [
[["32", "12", "56"], ["45", "3", "67"]],
[["60", "34", "2"], ["5", "13", "90"]]
]
我怎么做?这一行只提供行的列表
df.values.tolist()
IIUC
首先将整个数据帧转换为列表,然后将每个列表拆分为两个单独的列表
df2=df.values.tolist() #Convert the DataFrame to List
lst_split=[]
for lst in df2:
temp1=lst[:len(lst)//2] #Create First Half of the split
temp2=lst[len(lst)//2:] #Create Second Half of the split
lst_split.append(temp1)
lst_split.append(temp2) #Appending to the Final list
它给了我一个关于“时间”的错误。这可能不是索引吗?@HugoB你可以把它放在df=df.drop('time',1)加上一些解释也可以帮助更好地理解答案。
df2=df.values.tolist() #Convert the DataFrame to List
lst_split=[]
for lst in df2:
temp1=lst[:len(lst)//2] #Create First Half of the split
temp2=lst[len(lst)//2:] #Create Second Half of the split
lst_split.append(temp1)
lst_split.append(temp2) #Appending to the Final list