获取pandas中各列具有相同值的行

2024-04-24 15:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在pandas中,给定一个数据帧D:

+-----+--------+--------+--------+   
|     |    1   |    2   |    3   |
+-----+--------+--------+--------+
|  0  | apple  | banana | banana |
|  1  | orange | orange | orange |
|  2  | banana | apple  | orange |
|  3  | NaN    | NaN    | NaN    |
|  4  | apple  | apple  | apple  |
+-----+--------+--------+--------+

当有三列或更多列时,如何返回在其所有列中具有相同内容的行,以便它返回:

+-----+--------+--------+--------+   
|     |    1   |    2   |    3   |
+-----+--------+--------+--------+
|  1  | orange | orange | orange |
|  4  | apple  | apple  | apple  |
+-----+--------+--------+--------+

请注意,当所有值都为NaN时,它会跳过行。

如果这只是两列,我通常会D[D[1]==D[2]]但是我不知道如何将其推广到超过两列的数据帧。


Tags: 数据内容applepandasnanbananaorange
3条回答

我的条目:

>>> df
        0       1       2
0   apple  banana  banana
1  orange  orange  orange
2  banana   apple  orange
3     NaN     NaN     NaN
4   apple   apple   apple

[5 rows x 3 columns]
>>> df[df.apply(pd.Series.nunique, axis=1) == 1]
        0       1       2
1  orange  orange  orange
4   apple   apple   apple

[2 rows x 3 columns]

这是因为对行调用pd.Series.nunique会给出:

>>> df.apply(pd.Series.nunique, axis=1)
0    2
1    1
2    3
3    0
4    1
dtype: int64

注意:但是,这将保留看起来像[nan, nan, apple][nan, apple, apple]的行。通常我希望这样,但对于您的用例来说,这可能是错误的答案。

类似于Andy Hayden的回答,检查min是否等于max(那么行元素都是重复的):

df[df.apply(lambda x: min(x) == max(x), 1)]

我将检查每一行是否对其第一个元素equal

In [11]: df.eq(df[1], axis='index')  # Note: funky broadcasting with df == df[1]
Out[11]: 
      1      2      3
0  True  False  False
1  True   True   True
2  True  False  False
3  True   True   True
4  True   True   True

[5 rows x 3 columns]

如果行中的所有元素都为True,则行中的所有元素都相同:

In [12]: df.eq(df[1], axis='index').all(1)
Out[12]: 
0    False
1     True
2    False
3     True
4     True
dtype: bool

仅限于行和可选的dropna:

In [13]: df[df.eq(df[1], axis='index').all(1)]
Out[13]: 
        1       2       3
1  orange  orange  orange
3     NaN     NaN     NaN
4   apple   apple   apple

[3 rows x 3 columns]

In [14]: df[df.eq(df[1], axis='index').all(1)].dropna()
Out[14]: 
        1       2       3
1  orange  orange  orange
4   apple   apple   apple

[2 rows x 3 columns]

相关问题 更多 >