Geo*_*ler 3 python filter pandas
如何pandas.Series通过分位数仓来过滤分位数?我在这里的三种不同方法要么完全失败,要么导致空集。
所需的解决方案将使用 df.query()
df = pd.DataFrame({'my_series':[1,2,3,4,5,6,7]})
df['quantile'] = pd.qcut(df.my_series, [0,0.5,0.6,1])
print(df)
#df[df.quantile == '(4.6, 7.0]'] # fails with key error :false
df['string_quantiles'] = df['quantile'].astype(object)
print(df)
display(df[df['string_quantiles'] == '(4.6, 7.0]']) # no failure, but empty set
df.query("my_series == '(0.999, 4.0]'") # empty set
Run Code Online (Sandbox Code Playgroud)
添加astype转换
yourdf=df[df['string_quantiles'].astype(str)=='(4.6, 7.0]'].copy()
Out[60]:
my_series quantile string_quantiles
4 5 (4.6, 7.0] (4.6, 7.0]
5 6 (4.6, 7.0] (4.6, 7.0]
6 7 (4.6, 7.0] (4.6, 7.0]
Run Code Online (Sandbox Code Playgroud)
要么
df[df['quantile'].map(lambda x : x.left)==4.6].copy()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66 次 |
| 最近记录: |