mR.bLuEsKy
mR.bLuEsKy

Reputation: 43

How to filter with pandas dataframes vaules greater and less than a mean?

. I would like to filter in a data frame between 0.5 < mean value > 0.5 and merge both filtered data frames as a new data frame.

  1. Does < mean > work? any alternatives are also greatly appreciated
  2. Note that my original data frame is much greater than the sample given in the code.

I expect the output to be a new data frame filtered by mean_depth with 3 columns.

import pandas as pd
import numpy as np

data= {'x': [462574.63, 462617.91, 462614.76, 462621.02, 462624.16 ], 
       "Y": [5724781.1, 5724750.7, 5724745.7, 5724750.7, 5724755.7 ], 
       "depth": [32.75, 34.74, 35.30, 34.20, 33.73]}
df = pd.DataFrame(data)

df
#df.describe()
mean_depth = 34.144000
# filter to only show the rows in a  0.5 < mean_depth > 0.5 values
# Can this be automated? so that mean_depth not has to be inputed manualy?

Upvotes: 0

Views: 4699

Answers (2)

Vaishali
Vaishali

Reputation: 38415

IIUC, you need values within 0.5 of depth, you don't need to calculate mean separately,

data= {'x': [462574.63, 462617.91, 462614.76, 462621.02, 462624.16 ], 
       "Y": [5724781.1, 5724750.7, 5724745.7, 5724750.7, 5724755.7 ], 
       "depth": [32.75, 34.74, 35.30, 34.20, 33.73]}
df = pd.DataFrame(data)

new_df = df[df.depth.between(df.depth.mean() - 0.5, df.depth.mean() + 0.5)]

    x           Y           depth
3   462621.02   5724750.7   34.20
4   462624.16   5724755.7   33.73

Upvotes: 0

cwharris
cwharris

Reputation: 18125

Here's one way to accomplish a "not within 0.5 of the mean" filter.

import pandas
import numpy

data = {
    'x': [462574.63, 462617.91, 462614.76, 462621.02, 462624.16 ], 
    "y": [5724781.1, 5724750.7, 5724745.7, 5724750.7, 5724755.7 ], 
    "depth": [32.75, 34.74, 35.30, 34.20, 33.73]
}

df = pandas.DataFrame(data)

mean = df['depth'].mean()
mean_delta = 0.5
depth = df['depth']
above = depth > mean + mean_delta
below = depth < mean - mean_delta

df[above | below]

Upvotes: 1

Related Questions