1

I'm a newbie in python and building Neural Network regression model in python.

I'm trying to exclude rows that have zero values in the pandas.DataFrame, but don't know how to...

For example, if I have csv like below

input1   input2    input3   input4   input5        y
370     17.40013    8.9       4       740       883.0246
370     17.35865    8.9       4       740       884.0846
370     17.30227    0         4       740       884.9326
370     17.32991    8.9       4       740       884.4379
370     17.55929    0         4       740       883.1424
370     17.6505     8.9       4       740       883.1188

and want to exclude rows that have zero values (3rd and 5th row above example).

My code for now is including all rows of data as below code,

s1 = pd.Series(RMR_list)
s2 = pd.Series(PHT_list)
s3 = pd.Series(KLNF_list)
s4 = pd.Series(KLNM_list)
s5 = pd.Series(IDF_list)
s6 = pd.Series(CCN_list)

df = DataFrame({'RMR': RMR_list, 'PHT': PHT_list, 'KLN_F': s3.reindex(s1.index), 'KLN_M': s4.reindex(s1.index), 'IDF_M': s5.reindex(s1.index), 'CCN': s6.reindex(s1.index)})
df = df.values

#Setting training data and test data
train_size_x = int(len(df)*0.8)                     #The user can change the range of training data
print(train_size_x)
X_train = df[0:train_size_x, 1:6]
t_train = df[0:train_size_x, 0]
X_test = df[train_size_x:int(len(df)), 1:6]
t_test = df[train_size_x:int(len(df)), 0]

Using 80% of my whole data as training data and the rest of them are test data. And I'm trying to exclude rows that have zero values from those training and test data.

How should I implement in python code..?

ps. I'm using python 3.6

2
  • zero values in -any- columns? Commented Jul 14, 2017 at 8:20
  • @HoMan Yes, 0 in any columns :) Commented Jul 14, 2017 at 8:42

1 Answer 1

3

Try this:

df.loc[df.ne(0).all(axis=1)]

this will return only those rows that don't have zero (0) value in any column

if you want to delete those rows containing zero value:

df = df.loc[df.ne(0).all(axis=1)]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! I'll try out and let you know! :) I really appreciate it! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.