0

I've been going around in circles on this for a while and thought I'd post. I have a dataframe that has UserID, TimeSlot, and Count.

I'm trying to plot 2 UserIDs (3 & 4) on separate line charts, with TimeSlot along the x-axis, and a sum of count on the Y. I've tried referring to this excellent post but can't seem to get it right for my needs.

How can I fix this code?

userList = [3,4]

    df.set_index('TimeSlot', inplace=True)
    df2 = df.groupby('UserID')
    ncols=2
    nrows = int(np.ceil(df2.ngroups/ncols))

    fig, axes = plt.subplots(nrows=4, ncols=ncols, figsize=(12,4), sharey=True)

    for (key, ax) in zip(df2.groups.keys(), axes.flatten()):
        df2.get_group(key).plot(ax=ax)

    ax.legend()
    plt.show()
Reddspark
  • 6,934
  • 9
  • 47
  • 64

1 Answers1

0

Ok this was my eventual solution:

df.set_index('TimeSlot', inplace=True)
userList = [1,2,3]
df2 = df.loc[df['UserID'].isin(userList)]
df2 = df2.pivot(columns='UserID', values='Count')
df2.plot()

It actually plots the lines on the same chart which I decided was fine.

Reddspark
  • 6,934
  • 9
  • 47
  • 64