Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

matplotlib - Python Plot Data From CSV

enter image description here

Hi everyone, I'm trying to plot a graph data from CSV. There are 7 columns in my CSV. I've already plot the Genre column with my code:

import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import numpy as np
df = pd.read_csv('booksellers.csv')
genre = df['Genre']
countFiction = 0
countNonFiction = 0
for i in genre:
    if i == "Fiction":
        countFiction+=1
    else:
        countNonFiction+=1

labels = 'Fiction','Non Fiction'
sizes = [countFiction,countNonFiction]
fig1, ax1 = plt.subplots()
ax1.pie(sizes,labels=labels,startangle=90,autopct='%1.1f%%')
plt.show()

Now, I want to plot another 2 columns which are 'Author' and the average of 'User Rating'. If the Author is duplicated, how can I get only one Author with their average user rating? And also what kind of graph is compatible with it?

question from:https://stackoverflow.com/questions/65545877/python-plot-data-from-csv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
# you can iterate line by line 

from statistics import mean 

data = {}
for index, row in df.iterrows():
    author = row['Author']
    if not author in data:
        data[author] = {'rating':[]}
    data[author].append(row['User Rating'])

rates_by_authors = {}
for k in data.keys()
    rates_by_authors[k] = means(data[k])

# after create the data with that code

# you can use list(rates_by_authors.keys()) that is author's list as a X axis
# you can use list(rates_by_authors.values() ) that is average of ratings by authors list as a Y axis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...