assuming that your dataset is without header
class_label = dataset[:, -1] # for last column
dataset = dataset[:, :-1] # for all but last column
Answer from ahed87 on Stack Overflow Top answer 1 of 3
49
assuming that your dataset is without header
class_label = dataset[:, -1] # for last column
dataset = dataset[:, :-1] # for all but last column
2 of 3
1
In case you are interested in using numpy arrays, you can read your data in the csv file into a numpy array:
from numpy import genfromtxt
my_data = genfromtxt('E:\Book1.csv', delimiter=',', dtype = 'str', skip_header=1, unpack=True)
each item in my_data will be a list of each column in your csv file.
Now you can remove the last column by:
my_data_without_last_column = my_data[:-1].copy()
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.hsplit.html
numpy.hsplit โ NumPy v2.5 Manual
Split an array into multiple sub-arrays horizontally (column-wise).
05:10
How to Split an Array in NumPy? | NumPy Array Tutorials 2023 - ...
22:49
Python Numpy Tutorial: Numpy Splitting Array | Split | Array_Split ...
11:57
Slicing and Splitting Python NumPy Arrays - YouTube
05:16
Using NumPy split (...) and hsplit (...) functions : A Moment with ...
10:31
Python NumPy|Spliting Numpy Arrays | Python for Beginners | Learnerea ...
02:28
Python Numpy Tutorial Split Array - YouTube
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.split.html
numpy.split โ NumPy v2.5 Manual
Split array into multiple sub-arrays horizontally (column-wise).
MyCleverAI
mycleverai.com โบ it-questions โบ how-do-you-perform-a-split-operation-using-numpy-to-separate-data-into-x-and-y
How do you perform a split operation using NumPy to separate data into x and y?
- `data[:, -1]` selects all rows (`:`) and only the last column (`-1`), which is assigned to `y`. ... Print `x` and `y` to confirm the split. print("x (Features):", x) print("y (Target Variable):", y) This method is effective for separating features and target variables in a structured NumPy array, which is common in data science and machine learning tasks.
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.split.html
numpy.split โ NumPy v2.6.dev0 Manual
Split array into multiple sub-arrays horizontally (column-wise).
PythonForBeginners.com
pythonforbeginners.com โบ home โบ split a numpy array in python
Split a Numpy Array in Python - PythonForBeginners.com
September 16, 2024 - In the above example, we have split the input array at column index 2 and 5. Hence, the output array contains three sub-arrays. The first sub-array contains columns at index 0 and 1. The second sub-array contains columns from index 2 to 4. The last sub-array contains columns from index 5 to the last column. If columnindexN is greater than the length of the array, you can observe that the last sub-arrays will just be an empty numpy array.
Top answer 1 of 4
8
If you're using Numpy, first find the rows where the third column has your desired value, then extract the rows using indexing.
Demo
>>> import numpy
>>> A = numpy.array([[1, 0, 1],
[2, 0, 1],
[3, 0, 0],
[4, 0, 0],
[5, 0, 0]])
>>> A1 = A[A[:, 2] == 1, :] # extract all rows with the third column 1
>>> A0 = A[A[:, 2] == 0, :] # extract all rows with the third column 0
>>> A0
array([[3, 0, 0],
[4, 0, 0],
[5, 0, 0]])
>>> A1
array([[1, 0, 1],
[2, 0, 1]])
2 of 4
4
>>> a
array([[ 10., 15., 1.],
[ 21., 13., 1.],
[ 9., 14., 0.],
[ 14., 24., 1.],
[ 21., 31., 0.]])
>>> a[np.where(a[:,-1])]
array([[ 10., 15., 1.],
[ 21., 13., 1.],
[ 14., 24., 1.]])
>>> a[np.where(~a[:,-1].astype(bool))]
array([[ 9., 14., 0.],
[ 21., 31., 0.]])
Top answer 1 of 4
3
To make a list of arrays:
y = [x[x[:,3]==k] for k in np.unique(x[:,3])]
2 of 4
3
You can do this in O(NlogN) time using numpy.argsort, numpy.array_split, numpy.diff and numpy.where:
>>> indices = np.argsort(arr[:, 3])
>>> arr_temp = arr[indices]
>>> np.array_split(arr_temp, np.where(np.diff(arr_temp[:,3])!=0)[0]+1)
[array([[ 1. , 2. , 3. , 1. , 3. , 3. , 4. ],
[ 1.89, 2.3 , 1. , 1. , 3. , 3. , 4. ],
[ 1.1 , 2.1 , 1. , 1. , 3. , 3. , 4. ],
[ 1.9 , 2.2 , 1. , 1. , 3. , 3. , 4. ],
[ 1.3 , 2.2 , 1. , 1. , 3. , 3. , 4. ],
[ 1.5 , 2.1 , 1. , 1. , 3. , 3. , 4. ],
[ 1.4 , 2.3 , 1. , 1. , 3. , 3. , 4. ]]), array([[ 1.2 , 2.8 , 3.2 , 2. , 3.66 , 3.2 , 4.2 ],
[ 1.2 , 2.7 , 3.2 , 2. , 3.2 , 3.231, 4.2 ],
[ 1.2 , 2.9 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ],
[ 1.2 , 2.9 , 3.2 , 2. , 3.34 , 3.2 , 4.2 ],
[ 1.2 , 2.8 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ],
[ 1.2 , 2.7 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ],
[ 1.2 , 2.2 , 3.2 , 2. , 3.2 , 3.2 , 4.2 ]]), array([[ 1.3 , 2.3 , 3.6 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.89, 2.3 , 3.5 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.3 , 3.5 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.22, 3.6 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.3 , 3.3 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.99, 3.7 , 3. , 3.3 , 3.3 , 4.3 ],
[ 1.3 , 2.3 , 3.7 , 3. , 3.3 , 3.3 , 4.3 ]])]
NumPy
numpy.org โบ doc โบ 2.3 โบ reference โบ generated โบ numpy.split.html
numpy.split โ NumPy v2.3 Manual
Split array into multiple sub-arrays horizontally (column-wise).
Stack Overflow
stackoverflow.com โบ questions โบ 71428720 โบ split-numpy-array-by-column-value-while-keeping-track-of-row-indexs
python - Split Numpy array by column value, while keeping track of row indexs - Stack Overflow
Distances is now a list of numpy arrays of N length, where N is the number of possible values in the second column. I create a loop to split each array in distances, repeating the above step, however splitting by the last column this time like so:
DNMTechs
dnmtechs.com โบ extracting-last-column-from-pandas-dataframe-using-str-split-operation
Extracting Last Column from Pandas DataFrame using .str.split() Operation โ DNMTechs โ Sharing and Storing Technology Knowledge
Finally, we use .str[-1] to extract the last element of each split string and assign it to a new column โLast Columnโ. When working with real-world data, itโs common to encounter missing values (NaN) in DataFrame columns. To handle missing values while extracting the last column, we can ...
NumPy
numpy.org โบ doc โบ 2.0 โบ reference โบ generated โบ numpy.split.html
numpy.split โ NumPy v2.0 Manual
Split array into multiple sub-arrays horizontally (column-wise).
Codecademy
codecademy.com โบ article โบ split-numpy-arrays
How to Split Arrays in NumPy? | Codecademy
We can use the np.hsplit() function to split a NumPy array along the columns or the horizontal axis.
w3resource
w3resource.com โบ python-exercises โบ numpy โบ python-numpy-exercise-104.php
Python NumPy: Access last two columns of a multidimensional columns - w3resource
August 29, 2025 - The notation [:, [1, 2]] means that we are selecting all rows and the columns with indices specified in the list [1, 2]. Finally print() function prints the resulting array. ... Write a NumPy program to slice a 2D array and extract its last two columns using negative indexing.
Vultr Docs
docs.vultr.com โบ python โบ third party โบ numpy โบ split()
Python Numpy split() - Divide Array
January 1, 2025 - The example splits the matrix into three sub-arrays by cutting it just before columns 1 and 3. This results in a separation into columns 0; columns 1 and 2; and column 3. The split() function from NumPy offers a robust way to divide arrays into smaller sub-arrays, making it easier to manage large datasets or to assign specific sub-datasets to different processes or threads.