logo
down
shadow

delete rows in numpy array in python


delete rows in numpy array in python

Content Index :

delete rows in numpy array in python
Tag : python , By : lhoBas
Date : November 24 2020, 09:00 AM

around this issue In your proposed solution you are comparing with the integer 0, but the arrays consists of strings. So rows[1] == 0 will always give False. There's other things wrong with your solution as well.
Putting that aside, here's something that should work:
output = []
for C in my_input:
    mask = ~np.all(C[:, 1:] != '0', axis=1)
    output.append(C[mask])
output = [C[~np.all(C[:, 1:] != '0', axis=1)] for C in my_input]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to delete last n rows from Numpy array?


Tag : python , By : Nic Doye
Date : March 29 2020, 07:55 AM
Does that help You can use slice notation for your indexing.
To remove the last n rows from an array:
a = np.array(range(10)).reshape(5, 2)
>>> a
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

n = 2  # Remove last two rows of array.
>>> a[:-n, :]
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> a[n:, :]  # Remove first two rows.
array([[4, 5],
       [6, 7],
       [8, 9]])

Python array rows*1column numpy for numpy.dot(matrix,matrix2)


Tag : python , By : user106284
Date : March 29 2020, 07:55 AM
I hope this helps . Is a = [1, 2, 3] x = numpy.array(a) a matrix of 3 cols and 1 row? I know that x = numpy.array([a]) is a 1x4 matrix but i need the opossite. I need to multiply two matrix but the first one is a list inserted into a numpy.array(a) , It is a 1 dimensional array:
In [653]: x = np.array([1,2,3])
In [654]: x
Out[654]: array([1, 2, 3])
In [655]: x.shape
Out[655]: (3,)
In [656]: x.ndim
Out[656]: 1
In [657]: y = np.array([[1,2,3]])
In [658]: y
Out[658]: array([[1, 2, 3]])
In [659]: y.shape
Out[659]: (1, 3)
In [660]: y.ndim
Out[660]: 2
In [661]: z = y.T
In [662]: z
Out[662]: 
array([[1],
       [2],
       [3]])
In [663]: z.shape
Out[663]: (3, 1)
In [664]: np.dot(x,y)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-664-6849a5f7ad6c> in <module>()
----> 1 np.dot(x,y)

ValueError: shapes (3,) and (1,3) not aligned: 3 (dim 0) != 1 (dim 0)
In [665]: np.dot(y,x)
Out[665]: array([14])
In [666]: x*y   
Out[666]: array([[1, 4, 9]])
In [667]: x*z
Out[667]: 
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])
In [671]: x[:,None]
Out[671]: 
array([[1],
       [2],
       [3]])

In [672]: np.dot(x[:,None],y)
Out[672]: 
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

Delete all rows in a python numpy array that do not correspond to the top n values of a column


Tag : python , By : user152423
Date : March 29 2020, 07:55 AM
around this issue Since, the sorting needs to be done only on the 2nd column, you can use pandas. It makes the stuff a bit easier:
import pandas

In [725]: x
Out[725]: 
array([[0, 3],
       [1, 4],
       [2, 5],
       [3, 5],
       [2, 4]])

In [724]: pd.DataFrame(x).sort_values(1, ascending=False).head(3).values.tolist()
Out[724]: [[2, 5], [3, 5], [1, 4]]

Delete some rows of a numpy array


Tag : python , By : CM.
Date : March 29 2020, 07:55 AM
With these it helps I have a numpy array like below , Take advantage of numpy's vectorized methods. Say your array is a:
trimmed = cf[(cf != 0).any(axis=1)]

How to delete multiple rows of NumPy array?


Tag : python , By : Stephen
Date : March 29 2020, 07:55 AM
may help you . There are several ways to delete rows from NumPy array.
The easiest one is to use basic indexing as with standard Python lists:
>>> import numpy as np
>>> x = np.arange(35).reshape(7, 5)
>>> x
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
>>> result = x[5:]
>>> result
array([[25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
>>> x[:2, 1:4]
array([[1, 2, 3],
       [6, 7, 8]])
>>> x[[0, 2, 6]]
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [30, 31, 32, 33, 34]])
>>> np.take(x, [0, 2, 6], axis=0)
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [30, 31, 32, 33, 34]])
>>> np.delete(x, slice(0, 5), axis=0)
array([[25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
>>> np.delete(x, [0, 2, 3], axis=0)
array([[ 5,  6,  7,  8,  9],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
>>> mask_array = x[:, 0] < 12  # comparing values of the first column
>>> mask_array
array([ True,  True,  True, False, False, False, False])
>>> x[mask_array]
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> x[~mask_array]  # ~ is an element-wise inversion
array([[15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
Related Posts Related QUESTIONS :
  • How to store a word with # in .cfg file
  • How to append dictionaries to a dictionary?
  • How can I scrape text within paragraph tag with some other tags then within the paragraph text?
  • Custom entity ruler with SpaCy did not return a match
  • Logging with two handlers - one to file and one to stderr
  • How to do pivot_table in dask with aggfunc 'min'?
  • This for loop displays only the last entry of the student record
  • How to split a string by a specific pattern in number of characters?
  • Python 3: how to scrape research results from a website using CSFR?
  • Setting the scoring parameter of RandomizedSeachCV to r2
  • How to send alert or message from view.py to template?
  • How to add qml ScatterSeries to existing qml defined ChartView?
  • Django + tox: Apps aren't loaded yet
  • My css and images arent showing in django
  • Probability mass function sum 2 dice roll?
  • Cannot call ubuntu 'ulimit' from python subprocess without using shell option
  • Dataframe Timestamp Filter for new/repeating value
  • Problem with clicking select2 dropdownlist in selenium
  • pandas dataframe masks to write values into new column
  • How to click on item in navigation bar on top of page using selenium python?
  • Add multiple EntityRuler with spaCy (ValueError: 'entity_ruler' already exists in pipeline)
  • error when replacing missing ')' using negative look ahead regex in python
  • Is there a way to remove specific strings from indexes using a for loop?
  • select multiple tags by position in beautifulSoup
  • pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout
  • Shipping PyGObject/GTK+ app on Windows with MingW
  • Python script to deduplicate lines in multiple files
  • How to prevent window and widgets in a pyqt5 application from changing size when the visibility of one widget is altered
  • How to draw stacked bar plot from df.groupby('feature')['label'].value_counts()
  • Python subprocess doesn't work without sleep
  • How can I adjust 'the time' in python with module Re
  • Join original np array with resulting np array in a form of dictionary? multidimensional array? etc?
  • Forcing labels on histograms in each individual graph in a figure
  • For an infinite dataset, is the data used in each epoch the same?
  • Is there a more efficent way to extend a string?
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com