简体   繁体   中英

python dataframe iterate and plot/graph

Newbie question, I have a dataframe created from csv with approx 100K rows

    LINE    Line ID     HOURS   CONVERSION  Interval
0   BUR     0           42      75.00       48
1   SHK     1           15      100.00      24
2   BHH     14          16      65.16       24
3   GAT     2           71      50.00       72
4   MKT     23          60      100.00      60

I want to be able to iterate over each row and graph Interval on x axis and CONVERSION on y for each LINE/Line ID, whats the best way to do this?

You can simply use plot() method of dataframe if you just want to plot:

df.plot(x= 'Interval', y = 'CONVERSION')

results in:

在此处输入图像描述

If you want a separate line for each line, you can iterate over but you can opt-in for plotly which would be better and easier. And you get an interactive, more beautiful graph:

pd.options.plotting.backend = 'plotly'
df.plot(x= 'Interval', y = 'CONVERSION', color = 'Line')

在此处输入图像描述

PS

  • You do not need to iterate in pandas unless absolutely necessary. Operations are vectorized in their very nature.

  • I used extended version of your dataframe to demonstrate lines in plotly graph with last column mutated:

     LINE Line ID HOURS CONVERSION Interval 0 BUR 0 42 75.00 48 1 SHK 1 15 100.00 24 2 BHH 14 16 65.16 24 3 GAT 2 71 50.00 72 4 MKT 23 60 100.00 60 0 BUR 0 42 75.00 49 1 SHK 1 15 100.00 28 2 BHH 14 16 65.16 30 3 GAT 2 71 50.00 78 4 MKT 23 60 100.00 61

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM