Reputation: 2285
I have a time-series data which looks like this:
col1 col2
2000-01-01 00:00:00 A XX
2000-01-01 00:01:00 B YY
2000-01-01 00:02:00 A ZZ
2000-01-01 00:03:00 A XX
2000-01-01 00:04:00 B PP
What I am trying to achieve:
Groupby
by [col1, col2]
Get the size of each
Plot the results in plotly
using the chosen time-span
as the index
col1 col2
A XX 2
ZZ 1
B PP 1
YY 1
Basically plot this data with the chosen date range in plotly
Upvotes: 0
Views: 4049
Reputation: 31236
import plotly.express as px
df = pd.read_csv(io.StringIO(""" col1 col2
2000-01-01 00:00:00 A XX
2000-01-01 00:01:00 B YY
2000-01-01 00:02:00 A ZZ
2000-01-01 00:03:00 A XX
2000-01-01 00:04:00 B PP"""), sep="\s\s+", engine="python")
df = df.set_index(pd.to_datetime(df.index))
dfp = df.merge(
df.groupby(["col1", "col2"]).size().reset_index().rename(columns={0: "size"}),
on=["col1", "col2"],
).set_index(df.index)
px.bar(dfp, x=dfp.index, y="size", hover_data=["col1", "col2"])
Upvotes: 1