Matplotlib 和 DataFrame 错误 ValueError: The truth value of a DataFrame is ambiguous ||在 det

问题描述 投票:0回答:1

我正在尝试清理我的数据,该数据由 5 个 excel 文件(校准解决方案 1-5)组成,每个文件有多个列(传感器 1、传感器 2、传感器 3)到 DataFrames。问题(由于实验设置的性质)是在每个校准点文件中,传感器正在测量不同的校准溶液。所以我会有一个数据框calibration1:

传感器1解决方案1 传感器2解决方案2 传感器3解决方案3
180 12 300
182等 14等 302等

calibration2:等等

传感器1解决方案2 传感器2解决方案3 传感器 3 解决方案 1
12 304 179
13等 302等 178等

我在所有校准数据框中组合了相应的列,以便每个溶液都有一个数据框(pH),例如pH1

传感器1解决方案1 传感器2解决方案1 传感器 3 解决方案 1
180 181 179
182 180 178

参见示例calibration dataframe来自我的数据calibration1

datetime    TempC   Sensor1 Sensor2 Sensor3
2023-03-22 16:16:37 24.992  -177.3  309.0   175.7
2023-03-22 16:16:40 24.992  -177.3  309.0   175.7
2023-03-22 16:16:43 24.992  -177.3  309.0   175.8
2023-03-22 16:16:46 24.992  -177.3  309.0   175.7
2023-03-22 16:16:49 24.993  -177.3  309.0   175.7
2023-03-22 16:16:52     -177.4  309.0   175.7
2023-03-22 16:16:55 24.993  -177.4  309.0   175.7
2023-03-22 16:16:58 24.993  -177.4  309.0   175.7
2023-03-22 16:17:01 24.993  -177.4  309.0   175.7
2023-03-22 16:17:04     -177.4  309.0   175.7
2023-03-22 16:17:07 24.993  -177.4  309.0   175.7

和示例解决方案数据框:称为pH9

    Sensor1 Sensor2 Sensor3
0   311.3   309.2   313.0
1   311.4   309.2   313.0
2   311.3   309.2   313.0
3   311.3   309.2   313.0
4   311.3   309.2   313.0
5   311.4   309.1   313.0
6   311.3   309.2   313.0
7   311.3   309.2   313.0
8   311.3   309.1   313.0
9   311.3   309.1   312.9
10  311.3   309.1   313.0
11  311.3   309.2   313.0
12  311.3   309.1   313.0
13  311.3   309.1   313.0

我不知道我的分析过程中发生了什么,但现在我开始在计算平均值或绘制箱线图或小提琴图时收到错误消息。奇怪的是,我可以访问数据而且看起来还不错:

In [31]: pH9.Sensor1
Out[33]: 
0     -133.0
1     -133.0
2     -133.0
3     -133.0
4     -133.0
 
297      NaN
298      NaN
299      NaN
300      NaN
301      NaN
Name: Sensor1, Length: 302, dtype: float64

但是当我计算它的平均值时,我得到了这个巨大的错误:


pH9.Sensor1.mean()
Out[34]: -133.0027777777778

[SpyderKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\kernelbase.py", line 409, in dispatch_shell
    await result
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\kernelbase.py", line 798, in inspect_request
    reply_content = self.do_inspect(
                    ^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\ipkernel.py", line 555, in do_inspect
    bundle = self.shell.object_inspect_mime(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\interactiveshell.py", line 1838, in object_inspect_mime
    return self.inspector._get_info(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\oinspect.py", line 738, in _get_info
    info_dict = self.info(obj, oname=oname, info=info, detail_level=detail_level)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\oinspect.py", line 838, in info
    if info and info.parent and hasattr(info.parent, HOOK_NAME):
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\pandas\core\generic.py", line 1527, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

另外我应该可以画箱线图?我有一个包含列和浮点数据的数据框,为什么不可能?

我可以绘制:并得到一个箱线图

pH9.mean().plot(kind='bar')

barplot

但是使用 plt.violinplot() 我得到一个只有 1 把小提琴的 violinplot 和这个奇怪的在 det 警告中遇到的无效值。

In [42]: plt.violinplot(dataset=pH9)
C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\numpy\linalg\linalg.py:2139: RuntimeWarning:

invalid value encountered in det

Out[42]: 
{'bodies': [<matplotlib.collections.PolyCollection at 0x24a9526e490>,
  <matplotlib.collections.PolyCollection at 0x24a955b0090>,
  <matplotlib.collections.PolyCollection at 0x24a955bc310>],
 'cmaxes': <matplotlib.collections.LineCollection at 0x24a95436890>,
 'cmins': <matplotlib.collections.LineCollection at 0x24a955be9d0>,
 'cbars': <matplotlib.collections.LineCollection at 0x24a955bd490>}

violinplot

我试着看看我的数据框列是否有问题,它显示了预期的输出

但我又犯了这个大错误

In [43]: pH9.keys()
Out[43]: Index(['Sensor1', 'Sensor2', 'Sensor3'], dtype='object')
[SpyderKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\kernelbase.py", line 409, in dispatch_shell
    await result
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\kernelbase.py", line 798, in inspect_request
    reply_content = self.do_inspect(
                    ^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\ipkernel.py", line 555, in do_inspect
    bundle = self.shell.object_inspect_mime(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\interactiveshell.py", line 1838, in object_inspect_mime
    return self.inspector._get_info(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\oinspect.py", line 738, in _get_info
    info_dict = self.info(obj, oname=oname, info=info, detail_level=detail_level)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\oinspect.py", line 838, in info
    if info and info.parent and hasattr(info.parent, HOOK_NAME):
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\pandas\core\generic.py", line 1527, in __nonzero__
    raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我开始时感觉这并没有发生..我现在尝试更新conda,numpy,重新启动spyder,重新启动计算机。没有帮助:(

我能想到的唯一问题是,当我尝试连接数据列时出现问题,因为并非所有数据列的行数都相同(再次出现错误消息):

pH1 = sensor1.reset_index(drop=True).join(sensor2.reset_index(drop=True), how='outer').join(sensor3.reset_index(drop=True), how='outer')

In[44]: pH1.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 302 entries, 0 to 301
Data columns (total 3 columns):
 #   Column   Non-Null Count  Dtype  
---  ------   --------------  -----  
 0   Sensor1  277 non-null    float64
 1   Sensor2  182 non-null    float64
 2   Sensor3  302 non-null    float64
dtypes: float64(3)
memory usage: 9.4 KB

[SpyderKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\kernelbase.py", line 409, in dispatch_shell
    await result
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\kernelbase.py", line 798, in inspect_request
    reply_content = self.do_inspect(
                    ^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\ipykernel\ipkernel.py", line 555, in do_inspect
    bundle = self.shell.object_inspect_mime(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\interactiveshell.py", line 1838, in object_inspect_mime
    return self.inspector._get_info(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\oinspect.py", line 738, in _get_info
    info_dict = self.info(obj, oname=oname, info=info, detail_level=detail_level)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\IPython\core\oinspect.py", line 838, in info
    if info and info.parent and hasattr(info.parent, HOOK_NAME):
  File "C:\Users\CMerz\Anaconda3\envs\Working\Lib\site-packages\pandas\core\generic.py", line 1527, in __nonzero__
    raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
python pandas numpy matplotlib spyder
1个回答
0
投票

我遇到了和你一样的问题,真的很烦人。对我来说,安装早期版本的 Spyder 解决了这个问题(我安装了 Spyder 5.4.0)。您可以使用 anaconda 安装早期版本的 Spyder。我不认为Spyder的官方网站提供以前版本的下载。

我不知道到底发生了什么,因为我是 Python/Spyder 的新手,最近我一直在摆弄我的 MacBook,我不知道这是我的问题还是 Spyder 的问题。然而,这暂时解决了我的问题,现在我的 Spyder 运行良好。

© www.soinside.com 2019 - 2024. All rights reserved.