首页 > 解决方案 > 没有索引在列中 - 不能做单线箱线图

问题描述

到目前为止:

  1. 我加载了一个 csv 文件,没有标题。我输入了新的列名。

  2. 我剥离所有空间

  3. 我整理数据。R150全部出现。但我不能为我的Istwert列做箱线图。错误:

    “[Index([',Istwert'], dtype='object')] 中没有一个在 [columns] 中”

如果我保存 csv,我没有发现任何可疑之处。

有任何想法吗?

到目前为止的代码:

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import pandas as pd
    df = pd.read_csv("<FILELOCATION>", delimiter=";" , skiprows = 1, names=["BID","Testschritt","Testbeschreibung","Sollwert","Minimum","Maximum","Istwert","Einheit"])
    df = df.apply(lambda x: x.str.strip() if (x.dtype == "object") | (x.dtype == "float") else x)
    result = df.loc[df["Testschritt"] == "R150"]
    result.boxplot(column = ["Istwert"])

这是我的 CSV 数据: csv图片

这是我在箱线图之前的结果: 数据结果

标签: pythonpandasdataframedata-science

解决方案


始终以文本而不是图像的形式提供您的数据。OCR 很痛苦。

这对我有用。

  1. 你用的是什么版本的熊猫?
  2. 你的数据类型是什么?
result = pd.DataFrame(
{'idx': [226, 1070, 1914, 2758, 3602, 4446, 5290, 6134, 6978, 7822],
 'BID': [7249, 7326, 7327, 7328, 7329, 7330, 7331, 7332, 7333, 7333],
 'Testschritt': ['R150','R150',  'R150',  'R150',  'R150',  'R150',  'R150',  'R150',  'R150',  'R150'],
 'Testbeschreibung': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 'Sollwert': [22, 22, 22, 22, 22, 22, 22, 22, 22, 22],
 'Minimum': [19.8, 19.8, 19.8, 19.8, 19.8, 19.8, 19.8, 19.8, 19.8, 19.8],
 'Maximum': [24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2],
 'Istwert': [20953,  21002,  20838,  20827,  20879,  20942,  20999,  20855,  20969,  20874],
 'Einheit': ['KOhm',  'KOhm',  'KOhm',  'KOhm',  'KOhm',  'KOhm',  'KOhm',  'KOhm',  'KOhm',  'KOhm']}
).set_index("idx")
print(f"pandas: {pd.__version__}\n{result.dtypes}")
result.boxplot(column = ["Istwert"])

输出

pandas: 1.1.0
BID                   int64
Testschritt          object
Testbeschreibung      int64
Sollwert              int64
Minimum             float64
Maximum             float64
Istwert               int64
Einheit              object
dtype: object

在此处输入图像描述


推荐阅读