[英]Conditional dropna() pandas
I have a rather simple question: I want to drop rows from a DataFrame based on a condition.我有一个相当简单的问题:我想根据条件从 DataFrame 中删除行。 The DataFrame looks something like this:
DataFrame 看起来像这样:
Program act
Original RO A
Original RO nan
Followup RO B
Followup RO nan
Integral RO nan
I want to delete nulls for Original RO
and Integral RO
Programs only.我只想删除
Original RO
和Integral RO
程序的空值。 So it should look like this:所以它应该是这样的:
Program act
Original RO A
Followup RO B
Followup RO nan
I always have problems when trying to do some operations on a portion of my dataframe and keep the rest as is.在尝试对我的数据帧的一部分执行一些操作并将其余部分保持原样时,我总是遇到问题。
I tried this:我试过这个:
df.loc[df.Program.str.match('^(Original|Integral)')] = df.dropna()
But isn't working.但不工作。 What am I doing wrong?
我究竟做错了什么? Thanks in advance!
提前致谢!
You can check conditions with isin()
and isna()
and subset the dataframe.您可以使用
isin()
和isna()
检查条件并对数据帧进行子集化。
lst = ['Original RO', 'Integral RO']
df = df[~(df['Program'].isin(lst) & df['act'].isna())]
# Another one using drop.
# df = df.drop(df[(df['Program'].isin(lst) & df['act'].isna())].index)
print (df)
Program act
0 Original RO A
2 Followup RO B
3 Followup RO NaN
You can use Subset in the drop.na :您可以在 drop.na 中使用子集:
df.dropna(subset = cols, inplace=True) df.dropna(子集= cols,就地=真)
where cols is the name of column you want to check (to drop the line)其中 cols 是您要检查的列的名称(删除该行)
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html?highlight=dropna#pandas.DataFrame.dropna https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html?highlight=dropna#pandas.DataFrame.dropna
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.