Apply a threshold on a Pandas DataFrame column

Duccio Piovani

I have a Daframe that looks like this

In [52]: f
Out[52]:
Date
2015-02-23 12:00:00    0.172517
2015-02-23 13:00:00    0.172414
2015-02-23 14:00:00    0.172516
2015-02-23 15:00:00    0.173261
2015-02-23 16:00:00    0.172921
2015-02-23 17:00:00    0.172371
2015-02-23 18:00:00    0.176374
2015-02-23 19:00:00    0.177480
    ...

and I want to apply a threshold to the series so that is the values go below it I would just substitute the threshold's value to the actual one.

I am trying to definte a boolean dataframe like

Bool = f > Threshold

but I am not sure how to go on. Thanks in Advance.

EdChum

IIUC then the following should work:

f[f> Threshold] = some_val

Or you can use clip_upper:

f = f.clip_upper(Threshold)

This will limit the upper values to your threshold value

In [147]:
df[df['val'] > 0.175] = 0.175
df

Out[147]:
                          val
Date                         
2015-02-23 12:00:00  0.172517
2015-02-23 13:00:00  0.172414
2015-02-23 14:00:00  0.172516
2015-02-23 15:00:00  0.173261
2015-02-23 16:00:00  0.172921
2015-02-23 17:00:00  0.172371
2015-02-23 18:00:00  0.175000
2015-02-23 19:00:00  0.175000

In [149]:    
df['val'].clip_upper(0.175)

Out[149]:
Date
2015-02-23 12:00:00    0.172517
2015-02-23 13:00:00    0.172414
2015-02-23 14:00:00    0.172516
2015-02-23 15:00:00    0.173261
2015-02-23 16:00:00    0.172921
2015-02-23 17:00:00    0.172371
2015-02-23 18:00:00    0.175000
2015-02-23 19:00:00    0.175000
Name: val, dtype: float64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas dataframe - identify rows with value over threshold in any column

From Dev

Pandas DataFrame compare columns to a threshold column using where()

From Dev

Apply function on each column in a pandas dataframe

From Dev

pandas DataFrame, how to apply function to a specific column?

From Dev

Pandas dataframe apply function to entire column

From Dev

How to apply LabelEncoder for a specific column in Pandas dataframe

From Dev

Using pandas DataFrame.apply for column operations

From Dev

Pandas dataframe apply function to entire column

From Dev

Pandas dataframe apply function to column strings based on other column value

From Dev

Pandas - Create a new column with apply for float indexed dataframe

From Java

Pandas Dataframe Question: Apply function add new column with results

From Dev

Apply time shift on Pandas DataFrame from another column

From Dev

How to apply a function to every value in a column in a pandas dataframe?

From Dev

Python Pandas Apply Formatting to Each Column in Dataframe Using a Dict Mapping

From Dev

Pandas dataframe generate column with different row info, but no apply function

From Dev

Use pandas DataFrame / Panel .apply() together with index/column information

From Dev

Apply function on dataframe Column to get several other columns Pandas Python

From Dev

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

From Dev

Pandas DataFrame Apply Efficiency

From Dev

Pandas dataframe, how can I group by single column and apply sum to multiple column and add new sum column?

From Dev

pandas apply a new column

From Dev

DataFrame apply filtering by other column

From Java

Apply on *staggered* groups of Pandas DataFrame

From Dev

Apply permutation matrix to pandas DataFrame

From Dev

Apply split join Pandas DataFrame

From Dev

Passing a dataframe as an an argument in apply with pandas

From Java

Pandas apply based on column dtypes

From Dev

How do I put a series (such as) the result of a pandas groupby.apply(f) into a new column of the dataframe?

From Dev

pandas: apply a function to each unique element in a dataframe column and merge back the output

Related Related

  1. 1

    Pandas dataframe - identify rows with value over threshold in any column

  2. 2

    Pandas DataFrame compare columns to a threshold column using where()

  3. 3

    Apply function on each column in a pandas dataframe

  4. 4

    pandas DataFrame, how to apply function to a specific column?

  5. 5

    Pandas dataframe apply function to entire column

  6. 6

    How to apply LabelEncoder for a specific column in Pandas dataframe

  7. 7

    Using pandas DataFrame.apply for column operations

  8. 8

    Pandas dataframe apply function to entire column

  9. 9

    Pandas dataframe apply function to column strings based on other column value

  10. 10

    Pandas - Create a new column with apply for float indexed dataframe

  11. 11

    Pandas Dataframe Question: Apply function add new column with results

  12. 12

    Apply time shift on Pandas DataFrame from another column

  13. 13

    How to apply a function to every value in a column in a pandas dataframe?

  14. 14

    Python Pandas Apply Formatting to Each Column in Dataframe Using a Dict Mapping

  15. 15

    Pandas dataframe generate column with different row info, but no apply function

  16. 16

    Use pandas DataFrame / Panel .apply() together with index/column information

  17. 17

    Apply function on dataframe Column to get several other columns Pandas Python

  18. 18

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

  19. 19

    Pandas DataFrame Apply Efficiency

  20. 20

    Pandas dataframe, how can I group by single column and apply sum to multiple column and add new sum column?

  21. 21

    pandas apply a new column

  22. 22

    DataFrame apply filtering by other column

  23. 23

    Apply on *staggered* groups of Pandas DataFrame

  24. 24

    Apply permutation matrix to pandas DataFrame

  25. 25

    Apply split join Pandas DataFrame

  26. 26

    Passing a dataframe as an an argument in apply with pandas

  27. 27

    Pandas apply based on column dtypes

  28. 28

    How do I put a series (such as) the result of a pandas groupby.apply(f) into a new column of the dataframe?

  29. 29

    pandas: apply a function to each unique element in a dataframe column and merge back the output

HotTag

Archive