简体   繁体   中英

How can I fix data frame has no attribute plot

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')

It shows me that DataFrame has no attribute plot.

All data frame type is a string.

I commented the line colordfgroup.show() and corrected mean calculation

    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')

Here is a minimal example

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM