pfnuesel
pfnuesel

Reputation: 15310

Plot DataFrame row by row

I have a DataFrameGroupBy that is relatively small. Below is my whole data per day. Albeit there going to be a few years worth of days.

            day loc_source loc_target  count
0  1.566691e+09       east       east   1768
1  1.566691e+09       east       west    559
2  1.566691e+09    unknown       east     73
3  1.566691e+09    unknown       west    160
4  1.566691e+09       west       east     53
5  1.566691e+09       west       west   1563

Now I want to plot this: count vs. day, one line for each unique combination of loc_source and loc_target. 6 lines in total. 1 plot.

I can do this by iterating over the rows, but I do not believe this is the right way to do it. How can I plot this in matplotlib?

Edit: I cannot add the expected output, since it's a plot. But I can describe it: The plot should show numbers from 0 to 2000 on the y axis (label count), and a single datapoint on the x axis (1.55591e+09) (for now, of course this is going to change once I use the full dataset). There are 6 lines on the plot:

LEGEND       y VALUE
east east       1768
east west        559
unknown east      73
unknown west     160
west east         53
west west       1563

Upvotes: 1

Views: 56

Answers (1)

rje
rje

Reputation: 6418

You can use pivot to restructure your data first. First we create a new column with the east/west values:

df['eastwest'] = df['loc_source'] + df['loc_target']

Then we restructure your data and plot:

df.pivot(index='day', columns='eastwest', values='count').plot()

This moves the day into the index and creates columns for each combination of loc_source and loc_target. Calling plot will create a line per column where the day will be on the x axis and count on the y axis.

Upvotes: 1

Related Questions