Pandas(0.16.2)显示3行数据帧

时间:2015-08-11 17:28:29

标签: pandas ipython-notebook

我正在尝试将pandas数据帧的输出限制为前3行。但是我得到了所有500000个数据点的摘要。当我运行它而没有指定“Time [s]”作为索引它正常工作时我只获得3行数据。我正在运行Pandas 0.16.2和Python 3。

%matplotlib inline
import pandas as pd
from sys import platform as _platform
import matplotlib.pyplot as plt
pd.set_option('display.mpl_style', 'default') # Make the graphs a bit prettier
plt.rcParams['figure.figsize'] = (15, 5)

shot1 = "C:\\Users\ELEC-LAB_1\Documents\GitHub\Black Powder\BlackPowder_Shot-1.csv"
shot1_data = pd.read_csv(shot1, parse_dates=['Time [s]'], index_col='Time [s]') # Read the data using the time as the index.
shot1_data[:3]

时间[s] CH 1 [psi] CH 2 [psi] CH 3 [psi] CH 4 [ps] CH 5 [psi] CH 6 [psi] CH 7 [psi] CH8 [psi] CH 9 [ psi] CH 10 [psi] CH 16 [V]

-0.200000 -0.018311 -0.054932 -0.012207 -0.054932 -0.006104 -0.048828 -0.030518 -0.018311 0.030518 -0.018311 0.011597

-0.199998 0.006104 0.109863 0.048828 0.048828 -0.018311 -0.054932 0.042725 0.054932 -0.042725 0.024414 0.010986

-0.199996 0.012207 -0.042725 0.061035 -0.097656 0.067139 0.006104 -0.054932 -0.067139 -0.097656 -0.134277 0.010986

-0.199994 0.012207 -0.006104 -0.079346 0.036621 -0.036621 0.042725 0.006104 0.067139 0.012207 -0.042725 0.011597

-0.199992 0.006104 0.067139 0.091553 0.091553 0.024414 0.012207 0.097656 -0.030518 -0.024414 0.061035 0.010986

-0.199990 0.036621 0.006104 0.061035 0.109863 0.073242 0.067139 0.109863 -0.054932 0.158691 0.000000 0.011597

500000行×11列

1 个答案:

答案 0 :(得分:1)

您尝试使用无效的整数值对datetimeindex的df进行切片,这就是获得完整df的原因。

示例:

In [34]:
df = pd.DataFrame(index=pd.date_range(start=dt.datetime(2015,1,1), end=dt.datetime(2015,1,10)))
df[:3]

Out[34]:
Empty DataFrame
Columns: []
Index: [2015-01-01 00:00:00, 2015-01-02 00:00:00, 2015-01-03 00:00:00, 2015-01-04 00:00:00, 2015-01-05 00:00:00, 2015-01-06 00:00:00, 2015-01-07 00:00:00]

如果您使用headiloc[:3],则会获得所需的结果:

In [35]:
df.head(3)

Out[35]:
Empty DataFrame
Columns: []
Index: [2015-01-01 00:00:00, 2015-01-02 00:00:00, 2015-01-03 00:00:00]

In [36]:    
df.iloc[:3]

Out[36]:
Empty DataFrame
Columns: []
Index: [2015-01-01 00:00:00, 2015-01-02 00:00:00, 2015-01-03 00:00:00]

这就是为什么当您没有将参数index_col='Time [s]'传递给read_csv时,您的代码行将作为默认int64索引运行。