Home > other >  Plot a chart that represents all the prices
Plot a chart that represents all the prices

Time:10-17

I have a dataframe like this

ID   Price
A    $106.00
B    $156.00
C    $166.00
D    $106.00

I want to create a line chart, that represents all the prices. So I thought the y-axis would be the prices and the x-axis could be the grouped prices. So that you can see what the average price is, or where the most prices are. But how could I do that? I saw in the documentations that pandas has df.plot.line(), but my Price is a string and not an integer. And I need to group the prices. How could I generate a plot that represents all the prices?

df = pd.DataFrame({'id':['A', 'B', 'C', 'D'], 'price':['$106.00', '$156.00', '$166.00', '$106.00']})
 # df.plot.line()

CodePudding user response:

IIUC, you can change type of column price to numeric then plot like below:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'id':['A', 'B', 'C', 'D'], 
                   'price':['$106.00', '$156.00', '$166.00', '$106.00']})

df['price'] = pd.to_numeric(df.price.str[1:])

df.plot(x='id', y='price')
plt.show()

Output:

enter image description here

  • Related