Post Reply 
Python (Glitch) & 'plotly.express' library
10-14-2023, 03:02 PM
Post: #1
Python (Glitch) & 'plotly.express' library
Hello,


I'm trying to understand Python and more particularly the plotly.express library.

For different reasons, I'm using Glitch to run the program.

The program uses a CSV file called 'sales.csv'.

To simplify, let's say that this file has 3 columns (actually 5):

→ 'product' (A, B or C)
→ 'price'
→ 'qtty' (quantity)

With the following code, a HTML file is generated and, by launching it, I obtain a graphic (a pie):

PHP Code:
import plotly.express as px
import pandas 
as pd

data 
pd.read_csv('https://docs.google.com/ … … … ?output=csv')

figure px.pie(datavalues='qtty'names='product'title='Sold quantity by product')
figure.write_html('Sales-by-product.html')

print(
'sales-by-product succefully generated !'

Now I want to get the sales amount (price * quantity) for each product, but I don't know how to handle the "price * quantity" multiplication with plotly.express (or any other calculation).
I couldn't find an answer on the Internet.

Does anyone know this and can explain it to me?

Thanks for having read me.

Bruno
Sanyo CZ-0124 ⋅ TI-57 ⋅ HP-15C ⋅ Canon X-07 + XP-140 Monitor Card ⋅ HP-41CX ⋅ HP-28S ⋅ HP-50G ⋅ HP-50G
Find all posts by this user
Quote this message in a reply
10-14-2023, 03:58 PM
Post: #2
RE: Python (Glitch) & 'plotly.express' library
Is this a general python questions, or does it pertain to using python on an HP Prime?

Often, stating your platform makes it much easier for folks here to help you.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
10-14-2023, 04:36 PM
Post: #3
RE: Python (Glitch) & 'plotly.express' library
(10-14-2023 03:58 PM)rprosperi Wrote:  Is this a general python questions, or does it pertain to using python on an HP Prime?

Often, stating your platform makes it much easier for folks here to help you.

Thank you for your reply.

This is a general question about Python. I use the 'https://glitch.com/' platform on a PC.
I have no python pocket calculator at the moment, but maybe one day.

To solve my problem, I try to add a column to the CSV file.
I get what I want if the calculation is an addition.

But no other operation is allowed… no surprise: these are strings!

If only I had a solution to convert these strings in numbers…

Bruno
Sanyo CZ-0124 ⋅ TI-57 ⋅ HP-15C ⋅ Canon X-07 + XP-140 Monitor Card ⋅ HP-41CX ⋅ HP-28S ⋅ HP-50G ⋅ HP-50G
Find all posts by this user
Quote this message in a reply
10-14-2023, 05:38 PM
Post: #4
RE: Python (Glitch) & 'plotly.express' library
You'll want to use pandas to do the data manipulation, and then plotly to plot the results.

Say you have a data frame like

Code:
data = {
    'product': ['Laptop', 'Smartphone', 'Headphones', 'Monitor', 'Mouse'],
    'price': [1000, 800, 150, 300, 50],
    'quantity': [5, 10, 20, 8, 30]
}

To generate a "sales" column,

Code:
data["sales"] = data["price"] * data["quantity"]

And then you can plot the results with plotly express.

ChatGPT is fluent in pandas and very useful for learning how to do more sophisticated dataframe manipulations, or for remembering all the plotly incantations.
Find all posts by this user
Quote this message in a reply
10-14-2023, 06:17 PM
Post: #5
RE: Python (Glitch) & 'plotly.express' library
Thank you drbarnack for your help!

Finally, I began to suspect that my problem was with the header line of the file.
So I deleted this line… and it works.

But it's not very neat and I have to figure out how to do it better.
it is certainly possible to jump this line. How is my new challenge. Smile

Bruno
Sanyo CZ-0124 ⋅ TI-57 ⋅ HP-15C ⋅ Canon X-07 + XP-140 Monitor Card ⋅ HP-41CX ⋅ HP-28S ⋅ HP-50G ⋅ HP-50G
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)