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:
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()