sec*_*guy 1 python python-3.x pandas
我正在基于一个原始熊猫数据框创建 3 个熊猫数据框。我已经计算了与规范的标准偏差。
#Mean
stats_over_29000_mean = stats_over_29000['count'].mean().astype(int)
Run Code Online (Sandbox Code Playgroud)
152542
#STDS
stats_over_29000_count_between_std = stats_over_29000_std - stats_over_29000_mean
Run Code Online (Sandbox Code Playgroud)
54313
stats_over_29000_first_std = stats_over_29000_mean + stats_over_29000_count_between_std
Run Code Online (Sandbox Code Playgroud)
206855
stats_over_29000_second_std = stats_over_29000_first_std + stats_over_29000_count_between_std
Run Code Online (Sandbox Code Playgroud)
261168
stats_over_29000_third_std = stats_over_29000_second_std + stats_over_29000_count_between_std
Run Code Online (Sandbox Code Playgroud)
315481
这适用于从 df 获取 2 stds 下的所有行
#Select all rows where count is less than 2 standard deviations
stats_under_2_stds = stats_over_29000[stats_over_29000['count'] < stats_over_29000_second_std]
Run Code Online (Sandbox Code Playgroud)
接下来我想从 df 中选择所有行,其中 >=2 stds 且少于 3 stds
我试过了:
stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std < stats_over_29000_third_std]
Run Code Online (Sandbox Code Playgroud)
和
stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std && < stats_over_29000_third_std]
Run Code Online (Sandbox Code Playgroud)
但两者似乎都不起作用。
这是使用 2 个条件过滤 df 的方法:
df = pd.DataFrame([[1,2],[1,3],[1,5],[1,8]],columns=['A','B'])res = df[(df['B']<8) & (df['B']>2)]结果 :
A B
1 1 3
2 1 5
Run Code Online (Sandbox Code Playgroud)在你的情况下:
stats_2_and_over_under_3_stds = stats_over_29000[(stats_over_29000['count'] >= stats_over_29000_second_std) & (stats_over_29000['count'] < stats_over_29000_third_std)]
Run Code Online (Sandbox Code Playgroud)
Pandas 现在有Series.between(left, right, inclusive=True), 允许同时进行两种比较。
在你的情况下:
stats_2_and_over_under_3_stds = \
stats_over_29000[(stats_over_29000['count'].between(
stats_over_29000_second_std, stats_over_29000_third_std)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9888 次 |
| 最近记录: |