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
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')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With