Search on blog:

Python: What means [:,0] and [:,1] in numpy or pandas

It is a notation used in Numpy and Pandas.

It lets you access elements in 2D array without for-loop.

[ : , 0 ] means (more or less) [ first_row:last_row , column_0 ].

For 2D array/matrix/dataframe this gives all values in column 0 (from all rows).

col_0 = data[:,0]
col_1 = data[:,1]

For rows you can do similar

row_0 = data[0,:]
row_1 = data[1,:]

# or

row_0 = data[0]
row_1 = data[1]

In normal list you can use only single index or single slide in [ ] and for 2D list you would try to write it as [:][0] and [:][1] but it doesn't work as in numpy/pandas.

For rows you could do similar

row_0 = data[0][:]
row_1 = data[1][:]

# or

row_0 = data[0]
row_1 = data[1]

But for columns you need for-loop

col_0 = data[row[0] for row in data]
col_1 = data[row[1] for row in data]


Stackoverflow: plt.plot meaning of [:,0] and [:,1]

If you like it
Buy a Coffee