Python 绘制熊猫数据框的特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37333165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:14:06  来源:igfitidea点击:

Plot specific rows of a pandas dataframe

pythonpandasplot

提问by ahajib

I have a pandas dataframe with three columns and I am plotting each column separately using the following code:

我有一个包含三列的 Pandas 数据框,我使用以下代码分别绘制每一列:

data.plot(y='value')

Which generates a figure like this one:

这会生成一个像这样的数字:

enter image description here

在此处输入图片说明

What I need is a subset of these values and not all of them. For example, I want to plot values at rows 500 to 1000 and not from 0 to 3500. Any idea how I can tell the plot function to only pick those?

我需要的是这些值的一个子集,而不是全部。例如,我想在 500 到 1000 行而不是从 0 到 3500 绘制值。知道如何告诉绘图函数只选择那些吗?

Thanks

谢谢

回答by EdChum

use ilocto slice your df:

用于iloc切片您的 df:

data.iloc[499:999].plot(y='value')

this will slice from row 500 up to but not including row 1000

这将从第 500 行切片到但不包括第 1000 行

Example:

例子:

In [35]:
df = pd.DataFrame(np.random.randn(10,2), columns=list('ab'))
df.iloc[2:6]

Out[35]:
          a         b
2  0.672884  0.202798
3  0.514998  1.744821
4 -1.982109 -0.770861
5  1.364567  0.341882

df.iloc[2:6].plot(y='b')

enter image description here

在此处输入图片说明

回答by Abhishek Dutta

enter image description hereIn case you have two dataframes say, 'tst'and 'ptst'and want to display only certain values of both (same range in both)on the same graph. Following will help:

在此处输入图片说明如果您有两个数据,例如'tst''ptst'并且只想在同一图形上显示两者的某些值(两者范围相同)。以下将有所帮助:

import matplotlib.pyplot as plts

plts.figure(figsize=(10,5))
plts.plot(tst['Price'])
plts.plot(ptst['Price'])