python - pandas take k entries from head and tail -
how can k
(let's 100
) entries pandas.dataframe head , tail of dataframe? suggest 2 separate steps , merge operation, seems tedious. there better way? tried use df.iloc[:100,:-100]
, return first 100 records.
you can use iloc
numpy.r_
concanecate indices:
n = 3 df = df.iloc[np.r_[0:n, -n:0]]
sample:
np.random.seed(45) df = pd.dataframe(np.random.randint(10, size=(10,5))) print (df) 0 1 2 3 4 0 3 0 5 3 4 1 9 8 1 5 9 2 6 8 7 8 5 3 2 8 1 6 4 4 8 4 6 4 9 5 1 6 8 8 1 6 6 0 4 9 8 7 0 9 2 6 7 8 0 0 2 9 2 9 6 0 9 6 0 n = 3 df = df.iloc[np.r_[0:n, -n:0]] print (df) 0 1 2 3 4 0 3 0 5 3 4 1 9 8 1 5 9 2 6 8 7 8 5 7 0 9 2 6 7 8 0 0 2 9 2 9 6 0 9 6 0
Comments
Post a Comment