Skip to main content
 首页 » 编程设计

python之构建饼图

2024年11月24日9lexus

我正在尝试制作分类变量“回收”的饼图。此变量中有 8 个类别。我写了这段代码:

 data.plot.pie(y='Reclaim', figsize=(5, 5)) 

出现错误“TypeError: '<' not supported between instances of 'str' and 'int'” 你能解释一下这里不支持什么吗?这里有什么问题?当我纯粹处理 str 类别时,int 如何发挥作用?

这是我的 Jupyter notebook 中的粘贴内容:

> import pandas as pd 
> data=pd.read_csv("reclaims1.txt") 
> data.head() 
> data.info() 
<class 'pandas.core.frame.DataFrame'> 
 RangeIndex: 122 entries, 0 to 121 
 Data columns (total 3 columns):  
 Reclaim 122 non-null object 
  Person 122 non-null object 
  Processing time 122 non-null float64 
 dtypes: float64(1), object(2) 
 memory usage: 2.9+ KB 
> import matplotlib.pyplot as plt 
> import seaborn as sns 
%matplotlib inline data.plot.pie(y='Reclaim', figsize=(5, 5)) 

这是一些示例数据:

       data['Reclaim'].head(5) 
        0    Account closing 
        1               IBAN 
        2        Status info 
        3           Matching 
        4            Billing 
        Name: Reclaim, dtype: object 
 

我想看看例如“国际银行” 饼图上数据集中的类别。里面有8个类别 全部的。我想看看它们在那里出现的频率。

请您参考如下方法:

IIUC,我觉得你需要Series.value_counts第一的。尝试:

data.Reclaim.value_counts().plot(kind='pie', figsize=(5, 5))