Ssank
Ssank

Reputation: 3667

omitting row with empty field in pandas

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

Upvotes: 2

Views: 7699

Answers (1)

joris
joris

Reputation: 139172

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.

Upvotes: 4

Related Questions