Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dropna on specific rows

Tags:

python

pandas

import pandas as pd
df = pd.DataFrame({'ticker':['x','x','y','z','z'],
                   'bid':[1,2,np.nan,2,np.nan]})

Using pandas .dropna() is there anyway to drop the rows from a specified index range or subset of the data? For example in the DataFrame above, if I want to only drop rows in the index for where ticker equals 'z'. This would hopefully return:

ticker bid
x       1
x       2
y       np.nan
z       2
like image 283
Cr1064 Avatar asked Nov 22 '25 05:11

Cr1064


2 Answers

You can use dropna with mask and fillna:

df.mask(df.eq('z')).dropna(how='all').fillna({'ticker':'z'})

Output:

 ticker  bid
0      x  1.0
1      x  2.0
2      y  NaN
3      z  2.0

or

df.mask(df.eq('z')).dropna(how='all').mask(df.eq('z'),'z')
like image 60
ansev Avatar answered Nov 24 '25 23:11

ansev


One option is to just check the two conditions separately:

In [13]: df[(df['bid'].notnull()) | (df['ticker'] != 'z')]
Out[13]:

  ticker  bid
0      x  1.0
1      x  2.0
2      y  NaN
3      z  2.0
like image 37
Randy Avatar answered Nov 25 '25 00:11

Randy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!