enterML
enterML

Reputation: 2285

Plotly and pandas groupby

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:

  1. Groupby by [col1, col2]

  2. Get the size of each

  3. 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

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31236

  • the definition of what you are trying to achieve is sparse. So I've assumed a bar plot
  • it's a simple case of merging the results you want back, then a simple plot
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"])

enter image description here

Upvotes: 1

Related Questions