Python 从数据帧的列数据生成多个二维数组,并将其存储在数据结构中
我有几种这种数据帧的版本 我的想法是为每个时间步在2D网格/数组中构造每个单独的值列。这些2D数组应通过增加时间值进行排序,并存储为每个变量的单独数据集(熊猫或numpy??)。 这样我就可以调用该值并一直加载它的实例。如果我绘制这些连续的时间步长,它应该为每个变量提供2D空间的时间表示(2D空间在时间上的运动图像)Python 从数据帧的列数据生成多个二维数组,并将其存储在数据结构中,python,arrays,pandas,numpy,Python,Arrays,Pandas,Numpy,我有几种这种数据帧的版本 我的想法是为每个时间步在2D网格/数组中构造每个单独的值列。这些2D数组应通过增加时间值进行排序,并存储为每个变量的单独数据集(熊猫或numpy??)。 这样我就可以调用该值并一直加载它的实例。如果我绘制这些连续的时间步长,它应该为每个变量提供2D空间的时间表示(2D空间在时间上的运动图像) INDEX TIME ELEM var1 var2 var3 .... 0 0 h1 0.555 0.97 1.555 1
INDEX TIME ELEM var1 var2 var3 ....
0 0 h1 0.555 0.97 1.555
1 0 t5 0 0.8 1.2
2 0 y7 1 7 1
...
300 15 h1 0.6 0.477 0
301 15 t5 0.9 0.777 1
302 15 y7 0.555 0.897 5
...
800 23 h1 20 7 2
801 23 t5 5 7 5
802 23 y7 0.1 3 55
...
1010 58 h1 9 0.7 11
1011 58 t5 10 977 6
1012 58 y7 5 71 52
...
从层次上讲,我想要实现的基本上是这种数据结构,其中每个变量都存储在二维数组中:
- 完整数据集(数据帧)
- 子数据集版本(数据帧)
- 时间实例(数据帧)
- var1(二维阵列)
- var2(二维阵列)
- var3(二维阵列)
- 时间实例(数据帧)
- 子数据集版本(数据帧)
import pandas as pd
import numpy as np
# read the csv file
b = pd.read_csv('D:/myfile.csv', skipinitialspace=False, skiprows=0)
# remove possible empty spaces from the headers
b.rename(columns=lambda x:x.strip(), inplace=True)
# extract unique times and variable names
times = b.TIME.unique()
#make an empty list
listt = []
# for each time instance
for i in range(len(times)):
# generate the sub-dataset for each uniqe time
foo = b.loc[b.TIME==times[i]]
# re-extract the column names for each iteration
colnames=b.columns.unique().tolist()
# for each columnname in the dataset
for k in range(len(colnames)):
# reshape and assign the reshaped arrays inside the colnames list
colnames[k] = np.reshape(foo[colnames[k]], (-1,51))
# append each TIME instance with it's respective structured data to the list
listt.append(colnames)
# convert the generated list to a 4D panel
mypanel = pd.Panel4D(listt)
这样,我的索引是数字的,所以我无法保留唯一时间的实际值和不是最佳的列名。我觉得这可以做得更好,更有效,只是不知道怎么做
欢迎提出建议……:) 你能从你的样品中添加所需的输出吗?你看过熊猫的面板了吗?@jezrael我不确定我知道怎么做,这是我在寻找解决方案时遇到的问题的一部分…@Khris nope我没有。但我现在正在研究它们……甚至不知道它们存在我不确定,但也许多索引可以帮助-
df=df.set_index(['TIME','ELEM'))
您能从您的样本中添加所需的输出吗?您查看过panda的面板吗?@jezrael我不确定我是否知道如何做到这一点,这是我在找到解决方案时遇到的问题的一部分…@Khris nope我没有。但我现在正在研究它们……甚至不知道它们存在我不确定,但也许多索引可以帮助-df=df。设置索引(['TIME','ELEM'))