【问题标题】:Could not convert string to float -headers无法将字符串转换为浮点标头
【发布时间】:2018-11-13 19:13:21
【问题描述】:

虽然有很多关于此错误的主题,但我找不到任何可以帮助我的解决方案。我的 .csv Excel 文件中有标有底部、左侧和右侧的标题,当我尝试绘制它们时,我得到一个无法将字符串转换为 由于这些标题导致的文本错误。我怎么能解决这个问题?

import matplotlib.pyplot as plt
import numpy as np
# Read the input data only once
Bottom, Left, Right = np.loadtxt ("C:Data 2.csv", delimiter=",", skiprows=1, unpack=True)

# Plot in the first axis
ax1.plot(Bottom, Left, label='Pressure/area', color='b')
plt.show()

这是文件的样子:

【问题讨论】:

  • 当询问加载文本文件时遇到的错误时,您需要提供文本文件(它的最小版本)和特定于该问题的代码(不是一些不相关的绘图命令)。见minimal reproducible example

标签: python python-2.7 numpy matplotlib graph


【解决方案1】:

您可以采用的其他方法之一是使用 pd.read_csv 通过 pandas 读取 csv 文件。这将帮助您解决问题。 例如:- 如果我的文件路径是 'example/path/pathtofile'

import pandas as pd
filepath = 'example/path/pathtofile'
data = pd.read_csv(filepath)

【讨论】:

  • 您应该添加更多细节,例如一个小示例。只是指向其他地方的链接的答案并不总是足够有用。
猜你喜欢
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多