Question

For the given table, write a code in python to delete the row if the row...

For the given table, write a code in python to delete the row if the row has more than 70% of it's values as 0 or Nan

In the table there are 11 values in a row without considering ID and Name, so if there more than 7 values are null values then the row should be deleted.

**code should be in python using pandas library

ID Name a b c d e f g h i j k
7 gg 0.214 0.111 -0.151 0.151 0.176 0.345 -0.073 0.176 0.151 0.111 0.202
8 hh -0.119
10 jj -1.473 0.124 -0.62 0.322 0.029 -1.088
11 kk -0.76 0.465 0.454 -0.073
12 ll 0.546 -0.493
13 mm 0.189 -1.028 -0.534 0.202 0.029 0.848
15 oo 0.07 0.731 0.322 0.176 -1.183 -0.251

Homework Answers

Answer #1
import pandas as pd
df = pd.read_csv('C:\\Users\\flash\\Desktop\\sample.csv')
print(df)
mod = df;
count_nan = df.isnull().sum(axis=1) # count NaN value row wise
for i in range(count_nan.shape[0]):
    if (((count_nan[i])*100)/13) > 70:   # checking % Nan
        mod = df.drop(df.index[i])       # modified after delete a row
        print(mod)

Output:

end

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions