Search code examples
pythonpandashead

Easiest way to print the head of a data in python?


I'm not defining my array with pandas, I'm using numpy to do it and I would like to know if there is any other way to print the first 5 rows of a data. Using pandas this is how I would do it: print(data.head()).

This is how i defined my data:

with open('B0_25.txt', 'r') as simulation_data:
simulation_data = [x.strip() for x in simulation_data if x.strip()]

data = [tuple(map(float, x.split())) for x in simulation_data[2:100]]  

x = [x[1] for x in data]
y = [x[2] for x in data]
z = [x[3] for x in data]
mx = [x[4] for x in data]
my = [x[5] for x in data]
mz = [x[6] for x in data]

mydata = np.array([x, y, z, mx, my, mz])

Solution

  • You need the transpose of mydata, otherwise x, y, z, mx, my, mz are the rows rather than the columns.

    mydata = np.array([x, y, z, mx, my, mz]).T
    print(mydata[:5, :])