我试图使用str.find()
,但它不断地引发一个错误,我做错了什么?你知道吗
我有一个矩阵,第一列是数字,第二列是这些字母的缩写。缩写要么是ED,LI,要么NA,我正试图找到对应于这些字母的位置,这样我就可以绘制一个散点图,用颜色编码来匹配这3组。你知道吗
mat=sio.loadmat('PBMC_extract.mat') #loading the data file into python
data=mat['spectra']
data_name=mat['name'] #calling in varibale
data_name = pd.DataFrame(data_name) #converting intoa readable matrix
pca=PCA(n_components=20) # preforms pca on data with 20 components
pca.fit(data) #fits to data set
datatrans=pca.transform(data) #transforms data using PCA
# plotting the graph that accounts for majority of data and noise
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('Number of components')
plt.ylabel('Cumulative explained variance')
fig = plt.figure()
ax1 = Axes3D(fig)
#str.find to find individual positions of anticoagulants
str.find(data_name,'ED')
#renaming data for easiness
x_data=datatrans[0:35,0]
y_data=datatrans[0:35,1]
z_data=datatrans[0:35,2]
x2_data=datatrans[36:82,0]
y2_data=datatrans[36:82,1]
z2_data=datatrans[36:82,2]
x3_data=datatrans[83:97,0]
y3_data=datatrans[83:97,1]
z3_data=datatrans[83:97,2]
# scatter plot of score of PC1,2,3
ax1.scatter(x_data, y_data, z_data,c='b', marker="^")
ax1.scatter(x2_data, y2_data, z2_data,c='r', marker="o")
ax1.scatter(x3_data, y3_data, z3_data,c='g', marker="s")
ax1.set_xlabel('PC 1')
ax1.set_ylabel('PC 2')
ax1.set_zlabel('PC 3')
plt.show()
不断出现的错误如下:
File "/Users/emma/Desktop/Final year project /working example of colouring data", line 49, in <module>
str.find(data_name,'ED')
TypeError: descriptor 'find' requires a 'str' object but received a 'DataFrame'
正确的语法应该是
看看这里的例子
https://www.programiz.com/python-programming/methods/string/find
编辑1 虽然我刚刚注意到data\u name是一个pandas数据帧,所以这不起作用?你到底想做什么?你知道吗
你中断的函数调用甚至没有返回到变量中?所以很难回答你的问题?你知道吗
这个错误是因为
find
方法需要一个str
对象而不是DataFrame
对象。正如PiRK提到的,问题是您在这里替换data_name
变量:我认为应该是:
此外,尽管
str.find(data_name,'ED')
是有效的,但建议的方法是只传递以下搜索项:相关问题 更多 >
编程相关推荐