。我想在0.5 <平均值> 0.5之间的数据框中进行过滤 并将两个过滤后的数据帧合并为新的数据帧。
我希望输出是一个新的数据帧,该数据帧由3列的mean_depth过滤。
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?
答案 0 :(得分:1)
这是完成“不在平均值的0.5范围内”过滤器的一种方法。
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]
答案 1 :(得分:0)
IIUC,您需要0.5深度之内的值,不需要分别计算均值,
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