Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Format dataframe for Plotly graphs – Pandas python

I got a dataframe that has some columns, i need to use that data to plot a plotly line chart graph.

ORIGEN TIMESTAMP DATE
ORANGE 2023-02-03 00:00:00
ORANGE 2023-02-03 00:00:00
ORANGE 2023-02-10 00:00:00
APPLE 2023-02-24 00:00:00
APPLE 2023-04-18 00:00:00
APPLE 2023-04-18 00:00:00

I need to transform it to this form.

ORIGEN TIMESTAMP DATE COUNT
ORANGE 2023-02-03 00:00:00 2
ORANGE 2023-02-10 00:00:00 1
APPLE 2023-02-24 00:00:00 1
APPLE 2023-04-18 00:00:00 2

I tried using PD.MELT and groupby but it didnt work.
Im trying to replicate something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Plotly Graph that i need to replicate with my data.

Thanks!

>Solution :

data = {
    "ORIGEN": ["ORANGE", "ORANGE", "ORANGE", "APPLE", "APPLE", "APPLE"],
    "TIMESTAMP DATE": ["2023-02-03 00:00:00", "2023-02-03 00:00:00", "2023-02-10 00:00:00",
                       "2023-02-24 00:00:00", "2023-04-18 00:00:00", "2023-04-18 00:00:00"]
}

df = pd.DataFrame(data)
df["TIMESTAMP DATE"] = pd.to_datetime(df["TIMESTAMP DATE"])

# Transform the DataFrame
transformed_df = df.groupby(["ORIGEN", "TIMESTAMP DATE"]).size().reset_index(name="COUNT")

# Create a line chart using Plotly Express
fig = px.line(transformed_df, x="TIMESTAMP DATE", y="COUNT", color="ORIGEN")

# Show the plot
fig.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading