Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas (0.16.2) Show 3 Rows of Dataframe

I'm trying to limit the output of the pandas dataframe to the first 3 rows. However I get a summary of all 500000 data points. When I run this without specifying "Time [s]" as the index it works properly and I only get 3 rows of data. I'm running Pandas 0.16.2 and 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]

Time [s] CH 1 [psi] CH 2 [psi] CH 3 [psi] CH 4 [psi] CH 5 [psi] CH 6 [psi] CH 7 [psi] CH 8 [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 rows × 11 columns

like image 646
Shonn McNeill Avatar asked Dec 09 '25 07:12

Shonn McNeill


1 Answers

You're trying to slice a df which has a datetimeindex using integer values that are not valid which is why you get the full df.

Example:

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]

If you used head or iloc[:3] then you get the desired result:

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]

That is why when you don't pass param index_col='Time [s]' to read_csv your line of code works as a default int64 index is created for you.

like image 135
EdChum Avatar answered Dec 12 '25 12:12

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!