简体   繁体   English

Pandas dropna 在特定行上

[英]Pandas dropna on specific rows

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?使用 pandas .dropna()是否可以从指定的索引范围或数据子集中删除行? For example in the DataFrame above, if I want to only drop rows in the index for where ticker equals 'z' .例如,在上面的 DataFrame 中,如果我只想在索引中删除 ticker 等于'z'行。 This would hopefully return:这有望返回:

ticker bid
x       1
x       2
y       np.nan
z       2

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
df.loc[df.ticker == "z"] = df.loc[df.ticker == "z"].dropna()
df.dropna(subset=["ticker"])

Not sure if this is better for when I have more columns and need to specify if they have 2 missing nan's (using the .dropna(thresh=2) in the first drop)不确定当我有更多列并且需要指定它们是否缺少 2 个 nan 时这是否更好(在第一次删除中使用.dropna(thresh=2)

You can use dropna with mask and fillna :您可以将dropnamaskfillna一起使用:

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

Output: 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM