我正在尝试将一个CSV文件转换为numpy数组。当我试图指定所需的列时,系统提示该列在CSV文件中不存在。于是我检查了CSV文件,发现我在头部指定的名称前面被自动添加了一个#号。有没有办法可以避免这种情况呢?
以下是代码
np.savetxt(path13 + 'bkg_sample.csv', predictions, fmt = '%5.5f' ,delimiter = ',' , header='predict')
头部名称 – # predict
在jupyter中的错误 – ‘Dataframe’ object has no attribute ‘predict’
predict = pd.read_csv('/home/user1/AAAA/Predictions/Code testingbkg.csv',usecols=[2,3,4,5,6,7,8,9,10])predictions = model.predict(standardscaler.transform(predict))np.savetxt(path13+'bkg_sample.csv', predictions, fmt = '%5.5f',delimiter = ',',header='predict')true = pd.read_csv('/home/user1/AAAA/Predictions/bkg_sample.csv')true[true.predict>0.67] ##错误发生在这里
图片链接:
bkgsample : https://i.sstatic.net/i0f8Y.jpg
predict.csv : https://i.sstatic.net/93lZr.jpg
回答:
在这里找到了答案 https://stackoverflow.com/a/17361181/16355784
显然,因为该行被视为注释,所以会自动插入#号。如果你想避免这种情况,只需在savetxt中使用comment=''
即可。