学c的sjj 发表于 2020-11-12 13:06:20

我想在data后插入一个0,但是报错怎么办

import numpy as np
import pandas as pd

df = pd.read_csv(r'data.txt',sep='\t',encoding='utf8',index_col=0)
data = np.array(df,dtype=float)
data = data.T#将数组翻转
c = len(data)#求列的长度
for i in range(0,7):
b =( sum (data))/c#求平均数
pc = (((data-b)**2)/c)
pl = (sum(pc))**0.5
data= (data-b)/pl
data = data.T
x = data.shape
for a in range(0,c):
data = np.append(data,0,1)
print(data)
   


总是报错这个:all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 0 dimension(s)
tip:那个数据就是一个七行七列的数组,可以自行编制,主要是想解决插入的问题。。。

笨鸟学飞 发表于 2020-11-12 13:39:00

本帖最后由 笨鸟学飞 于 2020-11-12 13:41 编辑

numpy.append(arr,values,axis=None)
arr : array_like

Values are appended to a copy of this array.

values : array_like

These values are appended to a copy of"arr".It must be of the correct shape (the same shape as "arr", excluding "axis").If "axis" is not specified, "values" can be any shape and will be flattened before use.

这些值附加到"arr"的副本上。 它必须是正确的形状(与"arr"形状相同,不包括"轴")。 如果未指定"axis",则"values"可以是任何形状,并在使用前展平。

“与"arr"形状相同,不包括"轴"”,这句话的意思是说,一个数组两维,如果axis=0,则axis=1这一维形状要相同
=====================

import numpy as np

DYX = np.zeros((3,1))
HXH = np.ones((3,8))
XH = np.append(DYX, HXH,axis=1)

print(DYX) #(3,1)
"""
[

]
"""

print(HXH) # (3,8)
"""
[

]
"""

#最终结果:
print(XH)
"""
[

]
"""

print(XH.shape)#(3, 9)
#axis = 1,在第二维上拼接,所以说,(3,1)和(3,8)就变成了(3,9)
页: [1]
查看完整版本: 我想在data后插入一个0,但是报错怎么办