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
Plot specific rows of a pandas dataframe
提问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:
这会生成一个像这样的数字:
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 iloc
to 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')
回答by Abhishek Dutta
In 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'])