Sum of columns based on range of values of other columns in a Pandas dataframe

Amir

This is my dataframe:

df = pd.DataFrame({'sym': ['a', 'b'], 'vol_1': [100, 50], 'price_1': [5, 150], 'vol_2': [1500, 2000], 'price_2': [20, 175],
                   'vol_3': [123, 500], 'price_3': [22, 1000], 'min': [18, 150], 'max': [23, 176]})

I want to add a column that sums vol_1, vol_2, and vol_3 for each row if the price that is in the next column for each vol is in range of min and max cols. For example for the first row I want vol_2 and vol_3 because the prices are in range of min and max. My desired outcome looks like this:

 sym  vol_1  price_1  vol_2  price_2  vol_3  price_3  min  max  vol_sum
0   a    100        5   1500       20    123       22   18   23     1623
1   b     50      150   2000      175    500     1000  150  176     2050
sammywemmy

Reshape the data so you have individual columns for vol, price, min and max. Next, filter for only rows where price is between min and max, group by the sym column and append result to df.

df["vol_sum"] = (pd.wide_to_long(df,
                                 stubnames=["vol", "price"], 
                                 i=["sym", "min", "max"], 
                                 j="number", 
                                 sep="_")
                  .query("min <= price <= max", engine="python")
                  .groupby("sym")
                  .vol
                  .sum()
                  .array
                 )



   sym  vol_1   price_1 vol_2   price_2 vol_3   price_3 min max vol_sum
0   a   100      5      1500    20      123     22      18  23  1623
1   b   50      150     2000    175     500    1000     150 176 2050

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Python Pandas: Find Sum of Column Based on Value of Two other Columns

分類Dev

Creating a new column based on values from other columns in python pandas

分類Dev

Conditionally fill column based off values in other columns in a pandas df

分類Dev

Creating a new column based on the values of other columns

分類Dev

How to create sum of columns in Pandas based on a conditional of multiple columns?

分類Dev

How to create sum of columns in Pandas based on a conditional of multiple columns?

分類Dev

pandas slicing nested values along with other columns

分類Dev

drop columns in pandas dataframe based on mask

分類Dev

pandas dataframe column based on row and multiple columns

分類Dev

Assigning values to columns based on groups in pandas

分類Dev

Transpose DF columns based on column values - Pandas

分類Dev

Replacing values in pandas dataframe columns by criteria

分類Dev

custom function to filter values in pandas dataframe columns

分類Dev

Subtracting average values from columns in pandas DataFrame

分類Dev

How to assign minimum value based on lookup values in two other columns in pandas?

分類Dev

Merging two pandas dataframes with common values that are presented in one dataframe as columns and on the other are in rows

分類Dev

(Beginner SQL) Select rows based on the sum of other columns

分類Dev

How to drop Pandas dataframe columns based on another boolean array's column values?

分類Dev

Conditionally change the values of a Series based on the values of other columns

分類Dev

Pandas Groupby Sum to Columns

分類Dev

How to select rows based on specific conditions of a range of columns in pandas

分類Dev

Select slices/ range of columns for each row in a pandas dataframe

分類Dev

Creating a new column based on range values provided in other dataframe

分類Dev

Appending columns of DataFrame to other DataFrame

分類Dev

Add column to Data Frame based on values of other columns

分類Dev

Calculating Average in Google doc based on values in other columns

分類Dev

How to sum with conditions on other columns

分類Dev

pandas replace dataframe value by other columns value in the same row

分類Dev

pandas merge inner skip other columns in right dataframe

Related 関連記事

  1. 1

    Python Pandas: Find Sum of Column Based on Value of Two other Columns

  2. 2

    Creating a new column based on values from other columns in python pandas

  3. 3

    Conditionally fill column based off values in other columns in a pandas df

  4. 4

    Creating a new column based on the values of other columns

  5. 5

    How to create sum of columns in Pandas based on a conditional of multiple columns?

  6. 6

    How to create sum of columns in Pandas based on a conditional of multiple columns?

  7. 7

    pandas slicing nested values along with other columns

  8. 8

    drop columns in pandas dataframe based on mask

  9. 9

    pandas dataframe column based on row and multiple columns

  10. 10

    Assigning values to columns based on groups in pandas

  11. 11

    Transpose DF columns based on column values - Pandas

  12. 12

    Replacing values in pandas dataframe columns by criteria

  13. 13

    custom function to filter values in pandas dataframe columns

  14. 14

    Subtracting average values from columns in pandas DataFrame

  15. 15

    How to assign minimum value based on lookup values in two other columns in pandas?

  16. 16

    Merging two pandas dataframes with common values that are presented in one dataframe as columns and on the other are in rows

  17. 17

    (Beginner SQL) Select rows based on the sum of other columns

  18. 18

    How to drop Pandas dataframe columns based on another boolean array's column values?

  19. 19

    Conditionally change the values of a Series based on the values of other columns

  20. 20

    Pandas Groupby Sum to Columns

  21. 21

    How to select rows based on specific conditions of a range of columns in pandas

  22. 22

    Select slices/ range of columns for each row in a pandas dataframe

  23. 23

    Creating a new column based on range values provided in other dataframe

  24. 24

    Appending columns of DataFrame to other DataFrame

  25. 25

    Add column to Data Frame based on values of other columns

  26. 26

    Calculating Average in Google doc based on values in other columns

  27. 27

    How to sum with conditions on other columns

  28. 28

    pandas replace dataframe value by other columns value in the same row

  29. 29

    pandas merge inner skip other columns in right dataframe

ホットタグ

アーカイブ