计算未过滤的value_counts和熊猫中的过滤值

时间:2020-05-30 01:11:51

标签: python pandas

假设我有一个糕点数据框

       Pastry      Flavor      Qty
0     Cupcake      Cheese      3
1   Cakeslice   Chocolate      2
2        Tart       Honey      2
3   Croissant   Raspberry      1

我得到了每种糕点的特定风味的价值

df[df['Flavor'] == 'Cheese']['Pastry'].value_counts()
Cupcake    4
Tart       4
Cakeslice  3
Turnover   3
Creampie   2
Danish     2
Bear Claw  2

然后要获得该数量的百分位数,我可以这样做

df[df['Flavor'] == 'Cheese']['Pastry'].value_counts().describe(percentiles=[.75, .85, .95]) 

我会从完整的数据框中得到类似的东西

count    35.00000
mean     1.485714
std      0.853072
min      1.000000
50%      1.000000
75%      2.000000
85%      2.000000
95%      3.300000
max      4.000000

其中有干酪味的不同糕点总数为35,因此,在这35种糕点中分配的奶酪总量为35。平均数量为1.48,最大数量为4(杯子蛋糕和蛋art)等,等等。

我想做的是通过计算味精列中不是'Cheese'的所有其他值来降低第95个百分点,但是value_counts()只计算'Cheese'的那些值,因为我过滤了数据框。我还如何计算非奶酪行,以便我的百分位数下降并代表整个数据框中奶酪总数的分布?

这是示例输出:

Cupcake    4
Tart       4
Cakeslice  3
Turnover   3
Creampie   2
Danish     2
Bear Claw  2
Swiss Roll 1
Baklava    0
Cannoli    0

如果非奶酪风味糕点的数量为0,则从那里我可以得到百分位数,由于现在有0的值将其稀释,因此百分位数将减少。

1 个答案:

答案 0 :(得分:0)

我决定走很长的路要尝试解决这个问题,我的结果给了我与this问题相同的答案

如果有人好奇的话,这还有很长的路要走。

@EnableWebSecurity
public class SecurityConfiguration {

public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/api/v1/users/**")
           .authorizeRequests().anyRequest()
           .hasRole("USER").and().httpBasic();
    }

}

@Configuration
@Order(2)
public class MVCSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().and().authorizeRequests()
                .antMatchers("/resources/**", "/users/register", "/users/signup", "/users/confirm",
                        "/users/user-action", "/users/reset-password", "/confirm", "/webjars/**").permitAll()
        .antMatchers("/users/**").hasRole("USER")
        .and()
        .formLogin().usernameParameter("username").passwordParameter("password");
    }
}

}
相关问题