简体   繁体   English

从 Pandas Dataframe 中选择一列中具有相同值而另一列仅缺失的行

[英]Selecting rows from Pandas Dataframe with same values in one column that have only missing in another

In the following code, under column A, foo and tog have only missing values in column B. However, I can't simply use is_na() to filter all missing values, since there is one bar that has a missing value.在下面的代码中,在 A 列下,foo 和 tog 在 B 列中只有缺失值。但是,我不能简单地使用is_na()过滤所有缺失值,因为有一个 bar 具有缺失值。

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                          'tog', 'bar', 'bar'],
                   'B' : [np.nan, 2, np.nan, 4, np.nan, 6, np.nan],
                   'C' : [2.0, 5., 8., 1., 2., 9., 3.]})

I've tried with df.groupby('A').filter(df['B'] == 'NaN') , but that returns an error:我试过df.groupby('A').filter(df['B'] == 'NaN') ,但返回错误:

'Series' object is not callable. '系列' object 不可调用。

How can I filter or select for foo and tog?如何为 foo 和 tog 过滤或 select? Much appreciated!非常感激!

Edit: I'm cleaning a dataset that has a few missing values, but spread out amongst a lot of rows.编辑:我正在清理一个包含一些缺失值但分布在很多行中的数据集。 As such, I can't just simply select for named elements corresponding with column A (eg foo and tog).因此,对于与 A 列对应的命名元素(例如 foo 和 tog),我不能简单地使用 select。

In other words, I need the following换句话说,我需要以下

    A   B   C
1   bar 2.0 5.0
3   bar 4.0 1.0
5   bar 6.0 9.0
6   bar NaN 3.0

filter expects a function and you can pass one that checks if not all of the values in B are NaN : filter需要一个 function 并且您可以传递一个检查B中是否并非所有值都是NaN的值:

df.groupby("A").filter(lambda x: ~x.B.isna().all())

to get要得到

     A    B    C
1  bar  2.0  5.0
3  bar  4.0  1.0
5  bar  6.0  9.0
6  bar  NaN  3.0

where foo and tog are filtered out since they have all NaN's in B column.其中footog被过滤掉,因为它们在 B 列中具有所有 NaN。

暂无
暂无

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

相关问题 根据给定列 pandas 中的缺失值,将行从一个 dataframe 添加到另一个 - Add rows from one dataframe to another based on missing values in a given column pandas 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas 从Pandas Dataframe中找到列中的唯一值,然后查看这些值在另一列中是否具有相同的值 - From Pandas Dataframe find unique values in column and see if those values have the same values in another column 如何用 python 中另一个 dataframe 的值仅填充一个 dataframe 列中的缺失值? - How to fill only missing values in one dataframe column with values from another dataframe in python? 根据另一个 DataFrame 中的值选择行 - Selecting rows from one DataFrame depending on values from another 如何组合 pandas dataframe 中在一列中具有相同值的行 - How to combine rows in a pandas dataframe that have the same value in one column 熊猫:从另一个数据框中的列值插入数据框中的行 - pandas: insert rows in a dataframe from column values in another dataframe 从 Pandas DataFrame 中选择一列中具有相同值但另一列中具有不同值的行 - Select rows from a Pandas DataFrame with same values in one column but different value in the other column 在 Pandas DataFrame 中查找具有相同索引的一列中的唯一值 - Find unique values in one column that have the same index in Pandas DataFrame select 行来自 pandas dataframe 在另一列不同的列中具有相同值并找到平均值并使其成为字典 - select rows from pandas dataframe with same values in one column different on the other &find the average&make it a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM