简体   繁体   English

python dataframe 迭代和绘图/绘图

[英]python dataframe iterate and plot/graph

Newbie question, I have a dataframe created from csv with approx 100K rows新手问题,我有一个 dataframe 从 csv 创建,大约有 100K 行

    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?我希望能够为每个 LINE/Line ID 遍历每一行并在 x 轴上绘制 Interval 并在 y 上绘制 CONVERSION,最好的方法是什么?

You can simply use plot() method of dataframe if you just want to plot:如果您只想 plot,您可以简单地使用 dataframe 的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.如果您希望每行都有单独的行,您可以迭代,但您可以选择加入 plotly,这样会更好、更容易。 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.除非绝对必要,否则您不需要在 pandas 中进行迭代。 Operations are vectorized in their very nature.操作本质上是矢量化的。

  • I used extended version of your dataframe to demonstrate lines in plotly graph with last column mutated:我使用了 dataframe 的扩展版本来演示 plotly 图中的行,最后一列发生了突变:

     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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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