【问题标题】:# getting added to header in csv file# 被添加到 csv 文件的标题中
【发布时间】:2021-07-06 07:57:36
【问题描述】:

我正在尝试将 csv 文件转换为 numpy 数组。当我试图指定所需的列时,它只是说它在 csv 文件中不存在。所以我检查了 csv 文件,发现 # 被添加到我指定的标题名称中。 无论如何我可以避免这种情况吗?

代码如下

np.savetxt(path13 + 'bkg_sample.csv', predictions, fmt = '%5.5f' ,delimiter = ',' , header='predict')

标题名称 - # predict

jupyter 上的错误 - 'Dataframe' 对象没有属性 '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] ##This is where the error occurs

图片链接:

bkgsample:https://imgur.com/a/tzh0o2M

predict.csv : https://imgur.com/a/DhPAzqa

【问题讨论】:

  • 您尝试重命名列吗? df.columns = ["a", "b", "c"]
  • 是的,尝试使用 df.rename(columns = {'# predict' : 'predict'}) 。没用
  • 您好,您能否向我们展示您的预测数组样本并指定其形状,以及 bkg_sample.csv 文件前几行的样本?

标签: python csv machine-learning jupyter-notebook


【解决方案1】:

尝试列出 DataFrame 的列:

print(true.columns)

看起来在您的bkg_sample.csv 中没有名为predict 甚至# predict 的列。

【讨论】:

  • 当我运行这段代码时说Index(['# predict']) , dtype = 'object')
  • 您是否尝试过使用true[true['# predict']>0.67]
  • 另外,如果您想与浮点数进行比较,最好将此列的 dtype 更改为浮点数 - true['# predict'].astype(float)
【解决方案2】:

在这里找到答案https://stackoverflow.com/a/17361181/16355784

显然它会插入#,因为该行是注释,如果你想传递它,你只需要在 savetxt 中使用comment=''

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多