axay
axay

Reputation: 445

How to delete a specific row from the initial data based on a value in another array?

I hava a value [474] in form of array. I want to search for this value in 1st column of my data and delete that complete row, so my data size will reduce by 1 row.

import numpy as np
import io
from numpy import genfromtxt

data =io.StringIO("""
ID,1,2,3,4,5,6
5362,0.974,-0.404,-0.763,0.868,-0.5,0.16
485,-0.659,0.531,0.623,0.402,0.772,0.506
582,0.045,0.994,0.762,-0.036,0.117,-0.355
99,0.777,0.537,0.391,0.456,0.329,0.108
75,-0.44,0.522,0.856,-0.04,0.656,-0.935
474,0.357,0.81,0.135,0.389,0.055,0.224
594,-0.291,0.031,0.742,-0.332,0.815,0.983
597,0.968,-0.357,0.591,0.892,0.375,0.88
124,0.737,0.611,0.764,0.289,0.298,-0.705
635,0.883,0.96,-0.987,0.29,0.997,0.186
""")
data = genfromtxt(data, delimiter=',', skip_header=1, dtype=np.float64)
print(data)
diff = [474]
print (diff)
[474]

The below row should get deleted from the original array.

474,0.357,0.81,0.135,0.389,0.055,0.224

Upvotes: 0

Views: 35

Answers (2)

Ben Taylor
Ben Taylor

Reputation: 515

idx = np.where(data[:, 0] == 474)
cleaneddata = np.delete(data2, idx, 0)

Upvotes: 1

Natheer Alabsi
Natheer Alabsi

Reputation: 2870

Try this one:

data = genfromtxt(data, delimiter=',', skip_header=1, dtype=np.float64)
data = pd.DataFrame(data)
data[data[0] != 474]

Upvotes: 0

Related Questions