pandas Python从数组中删除括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51164408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Remove brackets from arrays
提问by Celine
I have a list that contains many arrays.
我有一个包含许多数组的列表。
coef
[array([[1.72158862]]),
array([[3.28338167]]),
array([[3.28004542]]),
array([[6.04194548]])]
Put it into dataframe gives:
将其放入数据框给出:
azone = pd.DataFrame(
{'zone': zone,
'coef': coef
})
zone coef
0 1 [[1.7215886175218464]]
1 2 [[3.283381665861124]]
I wonder if there are ways to remove brackets. I tried tolist() but not giving me a result.
我想知道是否有办法去除括号。我试过 tolist() 但没有给我结果。
Also for another list:
另外对于另一个列表:
value
[[array([8.46565297e-294, 1.63877641e-002]),
array([1.46912451e-220, 2.44570170e-003]),
array([3.80589351e-227, 4.41242801e-004])]
I want to have only keep the second value. desire output is:
我只想保留第二个值。欲望输出是:
value
0 1.63877641e-002
1 2.44570170e-003
2 4.41242801e-004
回答by min2bro
Using Ravel:
使用拉威尔:
coef = [np.array([[1.72158862]]),
np.array([[3.28338167]]),
np.array([[3.28004542]]),
np.array([[6.04194548]])]
coef = np.array(coef).ravel()
print(coef)
array([1.72158862, 3.28338167, 3.28004542, 6.04194548])
Furthermore, if you're not going to modify the returned 1-d array, I suggest you use numpy.ravel, since it doesn't make a copy of the array, but just return a view of the array, which is much faster than numpy.flatten
此外,如果您不打算修改返回的一维数组,我建议您使用 numpy.ravel,因为它不会制作数组的副本,而只是返回数组的视图,这要快得多比 numpy.flatten
回答by jpp
You can use NumPy's flattenmethod to extract a one-dimensional array from your list of multi-dimensional arrays. For example:
您可以使用 NumPy 的flatten方法从多维数组列表中提取一维数组。例如:
coef = [np.array([[1.72158862]]),
np.array([[3.28338167]]),
np.array([[3.28004542]]),
np.array([[6.04194548]])]
coef = np.array(coef).flatten()
print(coef)
array([1.72158862, 3.28338167, 3.28004542, 6.04194548])
Since NumPy arrays underly Pandas dataframes, you will find your Pandas coefseries will now be of dtype floatand contain only scalars.
由于 NumPy 数组是 Pandas 数据帧的基础,您会发现您的 Pandascoef系列现在将是 dtypefloat并且仅包含标量。

