0
dd=df.select(df.Color,df.ListPrice.cast("float"))
colordf = dd[['Color','ListPrice']]
colordfgroup = colordf.groupby('Color').mean('ListPrice')
colordfgroup.show()

my_plot = colordfgroup.plot(kind='bar')

它告诉我 DataFrame 没有属性图。

所有数据帧类型都是字符串。

4

1 回答 1

0

我评论了 colordfgroup.show() 行并修正了平均计算

    dd=df.select(df.Color,df.ListPrice.cast("float"))
    colordf = dd[['Color','ListPrice']]
    colordfgroup = colordf.groupby('Color')['ListPrice'].mean()
    #colordfgroup.show()

    my_plot = colordfgroup.plot(kind='bar')

这是一个最小的例子

import pandas as pd
import matplotlib.pyplot as plt
colors = ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D',  ]
lp     = [10.0, 20.0, 30.0, 40.0, 12.0, 22.0, 32.0, 42.0 ]
colordf = pd.DataFrame(data = {'Color': colors, 'ListPrice':lp}, index = [x for x in range(len(colors))])
colordfgroup = colordf.groupby('Color')['ListPrice'].mean()
my_plot = colordfgroup.plot(kind='bar')
于 2020-05-03T18:23:28.407 回答