简体   繁体   English

如何在python中删除数组的最后一列

[英]how to remove last column of an array in python

My array size is unknown and I would like to remove the very last column我的数组大小未知,我想删除最后一列

a = np.array([["A1","A2","A3"],["B1","B2","B3"],["C1","C2","C3"]])

I have tried我试过了

a[-1:]

but it deleted all rows except the last row但它删除了除最后一行之外的所有行

I also tried我也试过

a[:-1]

and it deleted the last row.它删除了最后一行。

How can I delete the last column?如何删除最后一列?

I suggest you to read the docs aboutBasic Slicing and Indexing the numpy array.我建议您阅读有关基本切片和索引numpy数组的文档。

Try this:尝试这个:

arr = a[:, :-1] #--> first two columns of array

Note 1: The resulting array arr of the slicing operation is just a view inside the original array a , no copy is created.注 1:切片操作的结果数组arr只是原始数组a内部的一个视图,没有创建副本。 If you change any entity inside the arr , this change will also be propagated in the array a and vice-a-versa.如果您更改arr任何实体,此更改也将在数组a传播,反之亦然。

For Example, Changing the value of arr[0, 0] will also change the corresponding value of a[0, 0] .例如,改变arr[0, 0]的值也会改变a[0, 0]的对应值。


Note 2: If you want to create a new array, while removing the last column so that changes in one array should not be propagated in other array, you can use numpy.delete which returns a new array with sub-arrays along an axis deleted.注意 2:如果要创建一个新数组,同时删除最后一列以便一个数组中的更改不应该在另一个数组中传播,则可以使用numpy.delete返回一个新数组,其中子数组沿删除的轴.

arr = np.delete(a, -1, axis=1) # --> returns new array

Output of >>> arr : >>> arr输出:

[['A1' 'A2']
 ['B1' 'B2']
 ['C1' 'C2']]

Try this:尝试这个:

import numpy as np
a = np.array([["A1","A2","A3"],["B1","B2","B3"],["C1","C2","C3"]])
print(a)

b = np.delete(a, np.s_[-1:], axis=1)
print(b)

Output:输出:

[['A1' 'A2' 'A3']
 ['B1' 'B2' 'B3']
 ['C1' 'C2' 'C3']]

[['A1' 'A2']
 ['B1' 'B2']
 ['C1' 'C2']]

If you wish to delete the last column如果您想删除最后一列

b = a[:,:2]
'''array([['A1', 'A2'],
   ['B1', 'B2'],
   ['C1', 'C2']], dtype='<U2')'''

if you wish to delete the last row如果你想删除最后一行

c = a[:2]
'''array([['A1', 'A2', 'A3'],
   ['B1', 'B2', 'B3']], dtype='<U2'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除 df 列 python 中最后一个句点之后的字符? - How to remove characters after last period in df column python? 如何使用python从列中的所有数据中删除最后一个字符? - How to remove the last character from all the data in a column using python? 如何从 python pandas 列中删除最后一个字母 - How to remove last letters from column python pandas 删除python数组中的最后一列 - Deleting the last column in a python array 如何在Python中将最后一列与2D数组分开? - How to separate last column from 2D-array in Python? 如果列中的第一个值大于last,则Numpy删除数组中的列 - Numpy remove column in array if first value in column is larger than last 如何将2D列表分为一个2D列表和一个数组(删除最后一列) - How to divide a 2D list into one 2D list and one array (remove the last column) 如何从 pandas (python) dataframe 的每一行/列中删除最后几个字符? - How to remove last few characters from each row/column from pandas (python) dataframe? 如何将最后一列(1d 数组)附加到 python 中的 2d numpy 数组? - How to append a last column (1d array) to a 2d numpy array in python? 如何使用python将数组(大小=(8,8,3))的最后一列的前8个元素替换为大小为(8,8)的二维数组 - how to replace the first 8 elements of last column of array(size = (8,8,3 )) with 2d array of size (8,8) using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM