Pandas dataframe apply calculations to selected rows

Testy8

I am trying to calculate outliers in a dataset using Median Absolute Deviation. My dataset is 3 columns, 2000 rows in the following format:

Km      Price   id
139000  8500    2010 holden cruze cdx jg auto
173000  8500    2010 holden cruze cdx jg auto
95000   8800    2008 honda civic vti-l auto
141000  8800    2010 holden cruze cdx jg auto
169078  8880    1999 mazda mx-5 manual

How can I do array calculations like the following below. I want each row to have a 'median' value, which should be the median of all rows with the same id.

model = '2010 holden cruze cdx jg auto'
data[data['id']==model]['median'] = data[data['id']==model]['Price'].median()

The below statement provides the right median for each model, I just dont know how to quickly apply it to every row in the 'median' column.

median = data[data['id']==model]['Price'].median()

Or, alternatively, is my approach to Median Absolute Deviation wrong - is there a quicker/easier way to do it with pandas/numpy?

Alexander

You can use transform which returns a series the same length as the dataframe:

df['median'] = df.groupby('id').Price.transform('median')

>>> df
       Km  Price                             id  median
0  139000   8500  2010-holden-cruze-cdx-jg-auto    8500
1  173000   8500  2010-holden-cruze-cdx-jg-auto    8500
2   95000   8800    2008-honda-civic-vti-l-auto    8800
3  141000   8800  2010-holden-cruze-cdx-jg-auto    8500
4  169078   8880         1999-mazda-mx-5-manual    8880

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to collectively set the values of multiple columns for certain selected rows for Pandas dataframe?

From Dev

pandas apply function that returns multiple values to rows in pandas dataframe

From Dev

How to apply Pandas Groupby with multiple conditions for split and apply multiple calculations?

From Dev

Intra-group calculations on Pandas dataframe

From Dev

pandas - how to combine selected rows in a DataFrame

From Dev

Python DataFrame - apply different calculations due to a column's value

From Dev

How to partition dataframe rows into bins with calculations

From Dev

python pandas dataframe : removing selected rows

From Dev

Instability of pandas dataframe calculations

From Dev

Pandas dataframe perform calculations on duplicate rows

From Dev

Apply function to pandas dataframe row using values in other rows

From Dev

Pandas: Append rows to DataFrame already running through pandas.DataFrame.apply

From Dev

Python Pandas - how to apply boolean series to extract rows from dataframe

From Dev

Using a shift() function within an apply function to compare rows in a Pandas Dataframe

From Dev

Splitting DataFrame rows with pandas

From Dev

Doing calculations on Pandas DataFrame with groupby and then passing it back into a DataFrame?

From Dev

How to iterate through selected rows in pandas dataframe with conditions matching three rows?

From Dev

Pandas DataFrames: Create new rows with calculations across existing rows

From Dev

How to collectively set the values of multiple columns for certain selected rows for Pandas dataframe?

From Dev

Summary calculations on a Pandas Dataframe

From Dev

Performing calculations on specific rows in dataframe and using the results to perform additonal calculations

From Dev

Intra-group calculations on Pandas dataframe

From Dev

Delete Rows in pandas dataframe

From Dev

Reference previous rows in Dataframe calculations (Pandas)

From Dev

Pandas DataFrame Apply Efficiency

From Dev

pandas dataframe add multiple rows for group of values with apply

From Dev

mapping selected rows in pandas dataframe

From Dev

DataFrame Split On Rows and apply on header one column using Python Pandas

From Dev

Iterate over rows in a pandas dataframe and apply a lambda function

Related Related

  1. 1

    How to collectively set the values of multiple columns for certain selected rows for Pandas dataframe?

  2. 2

    pandas apply function that returns multiple values to rows in pandas dataframe

  3. 3

    How to apply Pandas Groupby with multiple conditions for split and apply multiple calculations?

  4. 4

    Intra-group calculations on Pandas dataframe

  5. 5

    pandas - how to combine selected rows in a DataFrame

  6. 6

    Python DataFrame - apply different calculations due to a column's value

  7. 7

    How to partition dataframe rows into bins with calculations

  8. 8

    python pandas dataframe : removing selected rows

  9. 9

    Instability of pandas dataframe calculations

  10. 10

    Pandas dataframe perform calculations on duplicate rows

  11. 11

    Apply function to pandas dataframe row using values in other rows

  12. 12

    Pandas: Append rows to DataFrame already running through pandas.DataFrame.apply

  13. 13

    Python Pandas - how to apply boolean series to extract rows from dataframe

  14. 14

    Using a shift() function within an apply function to compare rows in a Pandas Dataframe

  15. 15

    Splitting DataFrame rows with pandas

  16. 16

    Doing calculations on Pandas DataFrame with groupby and then passing it back into a DataFrame?

  17. 17

    How to iterate through selected rows in pandas dataframe with conditions matching three rows?

  18. 18

    Pandas DataFrames: Create new rows with calculations across existing rows

  19. 19

    How to collectively set the values of multiple columns for certain selected rows for Pandas dataframe?

  20. 20

    Summary calculations on a Pandas Dataframe

  21. 21

    Performing calculations on specific rows in dataframe and using the results to perform additonal calculations

  22. 22

    Intra-group calculations on Pandas dataframe

  23. 23

    Delete Rows in pandas dataframe

  24. 24

    Reference previous rows in Dataframe calculations (Pandas)

  25. 25

    Pandas DataFrame Apply Efficiency

  26. 26

    pandas dataframe add multiple rows for group of values with apply

  27. 27

    mapping selected rows in pandas dataframe

  28. 28

    DataFrame Split On Rows and apply on header one column using Python Pandas

  29. 29

    Iterate over rows in a pandas dataframe and apply a lambda function

HotTag

Archive