用matplotlib做饼图,结果一直报错convert string to float,怎么解决

img


数据如上
用matplotlib做饼图,结果一直报错convert string to float,怎么解决
求改写or指导

你data中没有"商品类别"这个列名
你输出检查下data,看看有没有"商品类别"列名,data中 "商品类别"列名有没有写错,前后是不是有空格

你这里的data是pandas中的dataframe吗,如果是的话,可以试试用.loc[],示例如下:

import pandas as pd # pandas版本为1.1.5

data = pd.DataFrame({
    '商品类别': [1, 2, 3, 4, 5, 6],
    '抖音销量': [23, 45, 12, 98, 46, 66]
})


x = data['商品类别']
print(x)
"""
0    1
1    2
2    3
3    4
4    5
5    6
Name: 商品类别, dtype: int64
"""
x = data.loc[:, '商品类别']
print(x)
"""
0    1
1    2
2    3
3    4
4    5
5    6
Name: 商品类别, dtype: int64
"""
x = data.iloc[:,0] # 直接用列的数字索引
print(x)
"""
0    1
1    2
2    3
3    4
4    5
5    6
Name: 商品类别, dtype: int64
"""


打印一下data,看看data的数据结构和类型就能知道应该怎么取数据了,根据现在的信息无法确定怎么改

把你plt.pie()画饼图的命令贴一下,估计你是把索引当成数据了。

数据有问题,修正一下,看是不是有null之类的