let's say i have a dataset like this
age | id | BMI | insulin |
---|---|---|---|
15 | 0 | 30 | 0 |
11 | 30 | 17 | 30 |
27 | 15 | 30 | 0 |
5 | 21 | 30 | 45 |
0 | 21 | 30 | 45 |
21 | 21 | 0 | 45 |
now i want to delete the row that contains 0. I read all the data using panda now how can i delete these row from my dataset or after converting to a 2D array
CodePudding user response:
2 methods to achieve your goal:
# keep rows where all values != 0
df = df[df.ne(0).all(1)]
# or drop rows where at least one value == 0
df = df.drop(df[df.eq(0).any(1)].index)
Output:
>>> df
age id BMI insulin
1 11 30 17 30
3 5 21 30 45
CodePudding user response:
Filter rows with no 0
values per rows by compare by DataFrame.ne
and DataFrame.all
:
df = df[df.ne(0).all(axis=1)]
CodePudding user response:
You can loop through the dataset (I assume it's a list) and check to see if each item is 0. If it is not a zero you can add it to a new list ( a list with no 0s ). Example:
dataset = [age, id, BMI, insulin, 15, 0, 30, 0, 11, 30, 17, 30, 27, 15, 30, 0, 5, 21, 30, 45, 0, 21, 30, 45, 21, 21, 0, 45]
new_dataset = [] # new dataset which will only have values that are not zero
for data_point in dataset:
if data_point != 0:
new_dataset.append(data_point)
# our new dataset now contains all values from previous set without zeros