【问题标题】:Error when creating dataframe "*** ValueError: If using all scalar values, you must pass an index"创建数据框时出错“*** ValueError:如果使用所有标量值,则必须传递索引”
【发布时间】:2021-07-14 11:43:22
【问题描述】:

我试图用 seaborn 创建一个情节,但我遇到了一个错误:

*** ValueError: If using all scalar values, you must pass an index

在我的程序中,我从 XFOIL 读取了一个输出文件,并试图绘制其结果以进行检查。

XFOIL 文件格式:

#      x          Cp  
     1.00000    0.24276
     0.99818    0.23883
     0.99591    0.22657
     0.99342    0.21421
     0.99066    0.20128
     0.98759    0.18802
     0.98413    0.17434
     0.98020    0.16018
     0.97569    0.14544
     0.97044    0.12999
     0.96429    0.11374
     0.95703    0.09661
### **(the file is big, and I will not transcript it completely here)** ###

我决定创建一个数据框来轻松启用绘图过程。

lst = fl.readlines()
lst_splt = [ s.replace('#','').split() for s in lst] 
cp = pd.DataFrame.from_records(lst_splt[1:], columns=lst_splt[:1]).astype(float)

最后我尝试使用 seaborn 绘制它:

sns.lineplot(x='x',y='Cp', data=cp)

但正如我在问题开头所说的那样,出现了错误:

*** ValueError: If using all scalar values, you must pass an index

我可以做些什么来修复这个错误?

【问题讨论】:

  • 您可以尝试将 index=[0] 添加到参数 DataFrame.from_records(data, index=[0]) 中吗?我无法复制您的场景,但以前对我有用。
  • 您是否尝试检查 DataFrame 是否与您期望的一样?可以?您是否尝试检查(例如,通过阅读文档)Seaborn 是否希望以这种方式排列数据?当您尝试复制和粘贴 If using all scalar values, you must pass an index into a search engine 时发生了什么?
  • 您确定错误出现在sns.lineplot 上,而不是出现在外观不寻常的pd.DataFrame.from_records 上。你能在数据框创建后打印出cp.head()吗?
  • cp = pd.read_csv(flname,'r', sep='\s+',skiprows=(3), header=None, index=[0]) 错误*** TypeError: read_csv() got multiple values for argument 'sep' 在读取cp 时仍然存在。我没有设法通过这个修改到达sns.lineplot

标签: python pandas dataframe


【解决方案1】:

问题与传递给DataFrame 构造函数的columns 参数有关。

当打印 lst_splt[:1] 时,这是您传递给 columns 参数的值,我得到这个:

print(lst_splt[:1])
# [['x', 'Cp']]

在这种情况下,Dataframe 构造函数需要一个平面列表,而不是嵌套列表。当您将 lst_splt[:1] 更改为 lst_splt[:1][0] 时,问题就解决了,打印时当然会给出:

print(lst_splt[:1][0])
# ['x', 'Cp']
以下代码的修改版本可以正常工作:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

fl = open('data.txt', 'r')

lst = fl.readlines()
lst_splt = [ s.replace('#','').split() for s in lst]
cp = pd.DataFrame.from_records(lst_splt[1:], columns=lst_splt[:1][0]).astype(float)

sns.lineplot(data=cp, x='x',y='Cp')

plt.show()

出来:

【讨论】:

  • 我喜欢你提出的方法!非常感谢!它也会帮助我解决其他一些问题!
【解决方案2】:

不知道为什么会出现这个错误,但是你可以这样做:

import matplotlib.pyplot as plt

plt.plot(cp["x"], cp["Cp"])
plt.show()

编辑:经过一些实验,您创建数据框的方法似乎是罪魁祸首。您可以将其替换为:

cp = pd.read_csv(filename, sep="\s+", skiprows=2, names=["x", "Cp"])
# Make sure that you have the right value for skiprows (should ignore the header and that's it)

# Then this works:
sns.lineplot(x="x", y="Cp", data=cp)

【讨论】:

  • 在我看来,这并不能回答处理他的 XFOIL 文件格式的 OP 特定问题。
  • 使用您提出的第一种方法可以正常工作。我之前已经尝试过了。但我仍然对这个错误感到好奇。我尝试了类似于那种机智 seaborn 的方法,但错误仍然存​​在。
  • 是的,您是否尝试过用我的帖子中提到的函数pd.read_csv 替换文件打开> 读入数据框?这应该适用于您的文件格式并为您提供所需的结果
  • 关于第二个过程,我试过了,它成功了!非常感谢!
  • 删除“r”(第二个参数),它没有地方...
猜你喜欢
  • 2017-02-19
  • 2019-12-29
  • 1970-01-01
  • 2021-08-01
  • 2021-06-01
  • 1970-01-01
  • 2020-03-19
  • 2016-11-17
相关资源
最近更新 更多