Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy nd数组到不带[]的列_Python_Pandas_Numpy - Fatal编程技术网

Python Numpy nd数组到不带[]的列

Python Numpy nd数组到不带[]的列,python,pandas,numpy,Python,Pandas,Numpy,我正在尝试将numpy nd数组转换为pandas列, 但数据是用括号括起来的 这是我的np数组: array([[[ 7.10105920e+07], [ 9.18736320e+07], [ 8.35562800e+07], [ 7.16590640e+07], [ 8.28060960e+07], [ 6.77042000e+07], [ 7.07195360e+07],

我正在尝试将numpy nd数组转换为pandas列, 但数据是用括号括起来的

这是我的np数组:

array([[[  7.10105920e+07],
        [  9.18736320e+07],
        [  8.35562800e+07],
        [  7.16590640e+07],
        [  8.28060960e+07],
        [  6.77042000e+07],
        [  7.07195360e+07],
        [  1.04754616e+08],
        [  7.27420400e+07],
        [  7.33461760e+07],
        [  6.34156040e+07],
        [  8.00440800e+07],
这是我发送到数据帧的方式:

predictions = pd.DataFrame()
predictions['y_test'] = Y_test[0].tolist()
这就是我得到的:

           y_test
0    [71010592.0]
1    [91873632.0]
2    [83556280.0]
3    [71659064.0]
4    [82806096.0]
5    [67704200.0]
6    [70719536.0]
7   [104754616.0]
8    [72742040.0]
9    [73346176.0]

如何删除括号([])?

似乎您有一个3d阵列,您可以尝试:

predictions['y_test'] = Y_test[0,:,0]

predictions

#       y_test
#0  71010592.0
#1  91873632.0
#2  83556280.0
#3  71659064.0
#4  82806096.0
#5  67704200.0
#6  70719536.0
#7  104754616.0
#8  72742040.0
#9  73346176.0
#10 63415604.0
#11 80044080.0

它看起来像一个3D阵列。您可以将其第一个元素传递给DataFrame构造函数:

pd.DataFrame(Y_test[0], columns=['y_test'])
Out: 
         y_test
0    71010592.0
1    91873632.0
2    83556280.0
3    71659064.0
4    82806096.0
5    67704200.0
6    70719536.0
7   104754616.0
8    72742040.0
9    73346176.0
10   63415604.0
11   80044080.0
Divakar的更好选择是使用挤压:

pd.DataFrame(arr.squeeze(), columns=['y_test'])
Out: 
         y_test
0    71010592.0
1    91873632.0
2    83556280.0
3    71659064.0
4    82806096.0
5    67704200.0
6    70719536.0
7   104754616.0
8    72742040.0
9    73346176.0
10   63415604.0
11   80044080.0

有两种方法你可以做到

选项1。Numpy索引

predictions['y_test'] = Y_test[0,:,0]
选项2。使用列表理解展平

predictions['y_test'] = [x[0] for x in Y_test[0]]
选项3。Numpy展平功能

predictions['y_test'] = Y_test.flatten()

我宁愿使用
挤压
@divaker-yeah,也不愿费心去找出非单态暗物质。谢谢
prediction = pd.DataFrame(Y_test.flatten(), columns=['y_test'])
prediction.head()

    y_test
    0     1.0
    1     1.0
    2     1.0
    3     1.0
    4     1.0