2

I have a pandas dataframe

seq   value
ACCCT   1
ACTGS   2
        3
ACCTC   4

I want to omit the rows which have an empty field in the seq column. I tired the following but it did not work

   new_frame = pd.DataFrame(columns = design_frame.columns)
   for idx,row in design_frame.iterrows():
          seq = design_frame.ix[idx,'seq']

           if not seq == '':
                   new_row = design_frame.ix[idx,:]
                   new_frame = new_frame.append(new_row,ignore_index = True)


   design_frame = new_frame

1 Answer 1

4

You can use dropna for that if the you want to remove the empty rows:

df.dropna(subset=['seq'])

However, I think you do not have NaN values, but empty strings (''). In that case you eg do df[df['seq']!=''], or first convert the empty strings to NaN values (df[df['seq']==''] = np.nan)

In any case, you should try to avoid iterating over the dataframe for such cases.

Sign up to request clarification or add additional context in comments.

Comments

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.