从另一个Python Pandas数据框导入数据和列

时间:2016-01-07 03:05:49

标签: python pandas matrix correlation

我一直在尝试使用Pandas Python库选择相关矩阵的子集。 例如,如果我有一个类似

的矩阵
0 A B C  
A 1 2 3  
B 2 1 4  
C 3 4 1  

我可能想要选择一个矩阵,其中原始矩阵中的一些变量与其他一些变量相关联,如:

0 A C  
A 1 3  
C 3 1

为此,我尝试使用以下代码使用列表中所需变量的名称对原始相关矩阵进行切片,转置相关矩阵,重新分配原始列名称,然后再次切片。

data = pd.read_csv("correlationmatrix.csv")
initial_vertical_axis = pd.DataFrame()
for x in var_list:
    a = data[x]
    initial_vertical_axis = initial_vertical_axis.append(a)
print initial_vertical_axis
initial_vertical_axis = pd.DataFrame(data=initial_vertical_axis, columns= var_list)
initial_matrix = pd.DataFrame()
for x in var_list:
    a = initial_vertical_axis[x]
    initial_matrix = initial_matrix.append(a)
print initial_matrix  

但是,这会返回一个空的相关矩阵,其中包含正确的行和列标签,但没有像

这样的数据
0 A C  
A  
C  

我在代码中找不到导致此错误的错误。如果有一个更简单的方法,我愿意接受建议。

1 个答案:

答案 0 :(得分:0)

假设 public static void removeMiddle(int[] arr) { int size = arr.length; if(size % 2 ==0) { int x = arr.length/2 -1; for(int i = x; i <= arr.length - 2; i++) { arr[i] = arr[i + 2]; } } else { int z = arr.length/2; for(int i = z; i < arr.length - 1; i++) { arr[i] = arr[i + 1]; } } } 包含您的矩阵,

* 2.  If the size of the array happens to be even, do the following:
*   a.  Create a local variable that represents the first to remove by assigning it to be one less than
*       half the size of the array
*   b.  Run a for-loop starting at the value you just calculated (representing the first to remove) and
*       terminating at two less than the size of the array
*       i.  In the for-loop, assign the value at the current index of the array to be the value at two
*           more than the current index
* 3.  Otherwise, given the size of the array happens to be odd, do the following:
*   a.  Create a local variable that represents the first to remove by assigning it to be half the 
*       size of the array
*   b.  Run a for-loop starting at the value you just calculated (representing the first to remove) and
*       terminating at one less than the size of the array
*       i.  In the for-loop, assign the value at the current index of the array to be the value at one 
*           more than the current index