【问题标题】:How to Filter 2 parameters with scatterplot and pandas dataframe如何使用散点图和熊猫数据框过滤 2 个参数
【发布时间】:2022-08-19 01:39:52
【问题描述】:

我需要分离一些我得到的数据。我正在使用 pandas DataFrame 来做到这一点。

这是我的问题之前的代码:

import pandas as pd
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.svm import LinearSVC
from sklearn.metrics import ConfusionMatrixDisplay
arquivo_arff = arff.loadarff(r\"/content/Rice_MSC_Dataset.arff\")
dados = pd.DataFrame(arquivo_arff[0])
dados = dados[[\'MINOR_AXIS\', \'MAJOR_AXIS\', \'CLASS\']]

我已经完成了一个带有 5 个参数的散点图,以便使用此代码进行分析(0 个过滤器):

sns.scatterplot(
    data=dados, 
    x=\"MINOR_AXIS\", 
    y=\"MAJOR_AXIS\", 
    hue=\"CLASS\")
plt.show()

我的问题:我只需要过滤物种 b\'Basmati\' 和 b\'Ipsala\',但我无法做到这一点,我不知道为什么。

\"CLASS\" 参数为:b\'Basmati\',b\'Arborio\',b\'Jasmine\',b\'Ipsala\',bKaracadag\'

但是,在我使用的 \".arff\" 文件中,名称只有 \"Basmati,Arborio,Jasmine,Ipsala,Karacadag\"

我尝试过的: 仅过滤这两个物种,使用以下代码:

dados = dados[dados[\'CLASS\'].isin([\"\" \"b\'Arborio\'\" \"\", \"\" \"b\'Ipsala\'\" \"\"])]

没用。我怎样才能解决这个问题?

  • 使用.isin([\"Arborio\", \"Ipsala\"])].isin([b\"Arborio\", b\"Ipsala\"])] 有效吗?
  • .isin([b\"Arborio\", b\"Ipsala\"])] 工作。非常感谢!

标签: python pandas scatter-plot


【解决方案1】:

系统在某处向您显示类参数是b'Basmati'b'Arborio'b'Jasmine'b'Ipsala'b'Karacadag'。但是,这并不意味着参数实际上就是字符串中的这些字符。这些是包含参数的字符串的repr 表示。显然,这些字符串是byte strings,它是通过在字符串前面放置一个 b 来创建的,因此是奇怪的 repr 表示。

您的问题的解决方案是将字符串 "Arborio""Ipsala" 作为字节字符串提供给 pandas,方法是在它们前面放置一个 b:

dados = dados[dados['CLASS'].isin([b"Arborio", b"Ipsala"])]

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2018-09-28
    • 2017-05-07
    • 1970-01-01
    • 2021-04-05
    • 2017-11-30
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多