[英]Filtering nans on condition in pandas dataframe
I have a dataframe like this:我有一个像这样的 dataframe:
COL1 COL2
0 1 2
1 1 NaN
2 2 6
3 2 8
4 3 10
5 3 NaN
6. 4 NaN
As you can see there are duplicates values in Col1, I want a dataframe that is like this:如您所见,Col1 中有重复值,我想要一个像这样的 dataframe:
COL1 COL2
0 1 2
1 2 6
2 2 8
3 3 10
4 4 NaN
Basically if the same value in col1 has a nan value in col2 then I want to remove the row that has the NaN value.基本上,如果 col1 中的相同值在 col2 中具有 nan 值,那么我想删除具有 NaN 值的行。 However, I am not trying to move NaN values all together if that is the only value available in Col2 for a value in Col1
但是,如果这是 Col2 中唯一可用于 Col1 中的值的值,我不会尝试将 NaN 值一起移动
Therefore, I know I cannot use this:因此,我知道我不能使用它:
new_table = old_table[~old_table['COL2'].isna())
since this would remove all NaN
values which is not necessarily what I am looking for因为这将删除所有
NaN
值,这不一定是我正在寻找的
You can use pandas.DataFrame.duplicated
with pandas.DataFrame.isna
:您可以使用
pandas.DataFrame.duplicated
. 与pandas.DataFrame.isna
:
>>> df[~(df.COL1.duplicated(keep=False) & df.COL2.isna())]
COL1 COL2
0 1 2.0
2 2 6.0
3 2 8.0
4 3 10.0
6 4 NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.