Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot data from CSV
#1
Fruit Rate
Apple 4.7
Orange 4.6
Avocado 4.7
Cherry 4.7
Cherry 4.8
Apple 4.4
Banana 4.7
Banana 4.7
Orange 4.7

I have these two columns in the CSV file. I want to plot a graph with that two columns. But if the name of fruit is duplicate, only get one and their average rage (Distinct). For example, from my CSV file, the data will become

Fruit Rate
Apple 4.55
Orange 4.65
Avocado 4.7
Cherry 4.75
Banana 4.7

Then plotting the graph with new data.
Reply
#2
Hi @allen04,

It's worth if you can mention the exact error or problem you faced with your code.

May be following snippets help you.

import pandas as pd 

# Load the datasets
df1 = pd.DataFrame(data={"Fruit":['Apple','Orange','Avocado ','Cherry','Cherry','Apple','Banana','Banana','Orange'],
                          'Rate':[4.7,4.6,4.7,4.7,4.8,4.4,4.7,4.7,4.7]})

# Set the Fruit column as index
df1.set_index('Fruit', inplace=True)
# create a plot (a line chart)
df1.plot(kind='line')
Reply
#3
You can use groupby and mean

import pandas as pd 
import matplotlib.pyplot as plt

col_fruits = ['Apple','Orange','Avocado ','Cherry','Cherry','Apple','Banana','Banana','Orange']
col_rates = [4.7,4.6,4.7,4.7,4.8,4.4,4.7,4.7,4.7]
 
data={"Fruit":col_fruits, 'Rate':col_rates}
df1 = pd.DataFrame(data)
df1 = df1.groupby('Fruit', as_index=False).mean()
df1.set_index('Fruit', inplace=True)

print(df1)

df1.plot(kind='line')
plt.show()
Output:
Apple 4.55 Avocado 4.70 Banana 4.70 Cherry 4.75 Orange 4.65
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Likert survey data plot error Andrzej_Andrzej 6 1,303 Jul-16-2023, 10:11 PM
Last Post: deanhystad
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 801 May-29-2023, 02:42 PM
Last Post: Nietzsche
  Create simple live plot of stock data dram 2 2,856 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 894 Jan-21-2023, 05:01 PM
Last Post: nicholas
  How to plot intraday data of several days in one plot mistermister 3 2,854 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Creating Complex Color Spectrum Plot From Data JoeDainton123 2 2,076 Sep-15-2020, 08:09 AM
Last Post: DPaul
  How to plot data from live datasource? Makada 14 5,747 Sep-06-2020, 07:13 PM
Last Post: Makada
  Bode plot from time series experiment data discus 4 7,205 Jun-20-2020, 07:46 AM
Last Post: discus
  Plot data from list. Makada 0 1,755 Jan-22-2020, 10:58 AM
Last Post: Makada
  plot multiple employee sales data in a single graph pitanshu 0 1,882 Oct-24-2019, 01:56 PM
Last Post: pitanshu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020