Get quantile of column only if value of another column satisfies condition

mdawg

I have a dataframe with multiple columns. I want to a "double sort" where within the lowest 50%ile of column A, I extract the lowest 50%ile of column B.

A      B
3     1.0
5     2.0
7     0.5
9     2.1

In this example, the 50th percentile of A would give me the first two rows. Then, the 50th percentile of B of those two would be 1.5. Thus I should return something like column C:

A      B     C
3     1.0    True
5     2.0    False
7     0.5    False
9     2.1    False

In this manner, it is important that the third row does not become true.

Any help is much appreciated!

Allen

Is this what you are after?

(
    df.assign(C=df.A.lt(df.A.quantile(0.5)))
    .assign(C=lambda x: x.C & x.B.lt(x.loc[x.C].B.quantile(0.5)))
)

    A   B   C
0   3   1.1 False
1   5   0.9 True
2   7   2.0 False
3   9   2.1 False

The first assign creates a flag to indicate whether A is below 50% quantile.

The second assign does 2 things:

  1. Check if B is below the 50% quantile of the subset of B filtered by first condition
  2. フラグ(C)と上記の手順1の結果の論理ANDを実行し、列Cを更新します。

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Get value from another dataframe column based on condition

分類Dev

Check that a value can only exist once in a column per another column

分類Dev

Excel - Get value of a column in a row where match found in another column

分類Dev

Get the value of a column where one column is max and another is min

分類Dev

Copy value from one column to another based on condition (using pandas)

分類Dev

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

分類Dev

Value in one OR another column

分類Dev

Get max value of column for rows where a condition is met

分類Dev

Finding Duplicate Data Only Where Another Column Has a Different Value

分類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

select a column value that matches with a value of another column

分類Dev

Lookup for a value in column based on condition

分類Dev

Get values of one column based on another columns value

分類Dev

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

分類Dev

Mandatory column based on the value of another column

分類Dev

Compare two files having different column numbers and print the requirement to a new file if condition satisfies

分類Dev

SQL - Get column value based on another column's average between select rows

分類Dev

pandas groupby with condition on one column to populate another column

分類Dev

How to apply an operation in a column based on a condition from another column

分類Dev

how to add column in dataframe based on some condition from another column?

分類Dev

Get value by column name value

分類Dev

Auto-increment the value only when the value of another column changes sql server

分類Dev

Assign a value to column based on condition across rows

分類Dev

Update row value with column name if condition is met

分類Dev

Replacing a column value by another column value based on regex - Python

分類Dev

How to get column is Null and condition in php laravel

分類Dev

Creating an accumulative column based on another column that only accumulates for a new ID

分類Dev

SQL get value based on corresponding min/max(value) from column in another related table

分類Dev

Get the max value for a specific column

Related 関連記事

  1. 1

    Get value from another dataframe column based on condition

  2. 2

    Check that a value can only exist once in a column per another column

  3. 3

    Excel - Get value of a column in a row where match found in another column

  4. 4

    Get the value of a column where one column is max and another is min

  5. 5

    Copy value from one column to another based on condition (using pandas)

  6. 6

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

  7. 7

    Value in one OR another column

  8. 8

    Get max value of column for rows where a condition is met

  9. 9

    Finding Duplicate Data Only Where Another Column Has a Different Value

  10. 10

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

  11. 11

    select a column value that matches with a value of another column

  12. 12

    Lookup for a value in column based on condition

  13. 13

    Get values of one column based on another columns value

  14. 14

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

  15. 15

    Mandatory column based on the value of another column

  16. 16

    Compare two files having different column numbers and print the requirement to a new file if condition satisfies

  17. 17

    SQL - Get column value based on another column's average between select rows

  18. 18

    pandas groupby with condition on one column to populate another column

  19. 19

    How to apply an operation in a column based on a condition from another column

  20. 20

    how to add column in dataframe based on some condition from another column?

  21. 21

    Get value by column name value

  22. 22

    Auto-increment the value only when the value of another column changes sql server

  23. 23

    Assign a value to column based on condition across rows

  24. 24

    Update row value with column name if condition is met

  25. 25

    Replacing a column value by another column value based on regex - Python

  26. 26

    How to get column is Null and condition in php laravel

  27. 27

    Creating an accumulative column based on another column that only accumulates for a new ID

  28. 28

    SQL get value based on corresponding min/max(value) from column in another related table

  29. 29

    Get the max value for a specific column

ホットタグ

アーカイブ