Group values together in Pandas column, then filter values in another column

pymat

Pandas Dataframe looks like so:

Col1 Col2

A    1
A    1
A    1
B    0
B    0
B    1
B    1
B    1
C    1
C    1
C    1
C    1

I wanted to group all together in Col1, then check Col2 to see whether all values for that group i.e. A are 1. In this example the desired output would be:

[A, C]

(because only A and C have all values set to 1). How do I do this?

BEN_YO

In your case groupby with all

df.groupby('Col1').Col2.all().loc[lambda x : x ].index.tolist()
Out[350]: ['A', 'C']

Or without groupby

df.loc[~df.Col1.isin(df.Col1[df.Col2.eq(0)]),'Col1'].unique()
Out[352]: array(['A', 'C'], dtype=object)

From the comment

cs95 :df.loc[df['Col2'].astype(bool).groupby(df['Col1']).transform('all'), 'Col1'].unique()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Pandas group by using values in a Column

分類Dev

pandas sort a column by values in another column

分類Dev

Assign values in pandas column based on another column

分類Dev

Populating a column based on values in another column - pandas

分類Dev

Pandas group by several values in a single column

分類Dev

MongoDB - Getting distinct values of a column after a group by on another column is applied

分類Dev

How to sum the values in a column based on another column or different group?

分類Dev

Assign values in Pandas to a column based on variables from another column

分類Dev

Check if values of a column is in another column array in a pandas dataframe

分類Dev

pandas replace values based on values from another column

分類Dev

Filter group of rows based on sum of values from different column

分類Dev

Conditional values for column in pandas

分類Dev

Is it possible to find and filter records with one column that has matching values but unmatching values in another column?

分類Dev

Filter by multiple values on the same column

分類Dev

Filter on reduced values from a column?

分類Dev

Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

分類Dev

Generate percentage for each group based on column values using Python pandas

分類Dev

How to create new values in a pandas dataframe column based on values from another column

分類Dev

Copy values from one column to another using Pandas

分類Dev

pandas assign columns values depend on another column in the df

分類Dev

Pandas mean for certain columns (binary values) based on another column

分類Dev

Pandas: Sort number of words in one column by the values of another

分類Dev

Column to aggregate values in another one

分類Dev

color a column based on the values of another column with formattable

分類Dev

Categorise column based upon another column values

分類Dev

Slicing values in a column to make a condition for another column

分類Dev

SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

分類Dev

How to replace all same values of a group in a column (dataframe) according to another column without loop?

分類Dev

For each group in a column, get only the rows with values in another column closest to a defined set

Related 関連記事

  1. 1

    Pandas group by using values in a Column

  2. 2

    pandas sort a column by values in another column

  3. 3

    Assign values in pandas column based on another column

  4. 4

    Populating a column based on values in another column - pandas

  5. 5

    Pandas group by several values in a single column

  6. 6

    MongoDB - Getting distinct values of a column after a group by on another column is applied

  7. 7

    How to sum the values in a column based on another column or different group?

  8. 8

    Assign values in Pandas to a column based on variables from another column

  9. 9

    Check if values of a column is in another column array in a pandas dataframe

  10. 10

    pandas replace values based on values from another column

  11. 11

    Filter group of rows based on sum of values from different column

  12. 12

    Conditional values for column in pandas

  13. 13

    Is it possible to find and filter records with one column that has matching values but unmatching values in another column?

  14. 14

    Filter by multiple values on the same column

  15. 15

    Filter on reduced values from a column?

  16. 16

    Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

  17. 17

    Generate percentage for each group based on column values using Python pandas

  18. 18

    How to create new values in a pandas dataframe column based on values from another column

  19. 19

    Copy values from one column to another using Pandas

  20. 20

    pandas assign columns values depend on another column in the df

  21. 21

    Pandas mean for certain columns (binary values) based on another column

  22. 22

    Pandas: Sort number of words in one column by the values of another

  23. 23

    Column to aggregate values in another one

  24. 24

    color a column based on the values of another column with formattable

  25. 25

    Categorise column based upon another column values

  26. 26

    Slicing values in a column to make a condition for another column

  27. 27

    SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

  28. 28

    How to replace all same values of a group in a column (dataframe) according to another column without loop?

  29. 29

    For each group in a column, get only the rows with values in another column closest to a defined set

ホットタグ

アーカイブ