小编Sku*_*Pak的帖子

熊猫 value_counts() + 权重

我有一个像这样的 Pandas 数据框:

df = pd.DataFrame({'id': [121, 34324, 111, 12, 45, 232],
              'weight': [10, 2, 80, 49, 71, 18],
              'var_bool': [True, True, False, True, False, True],
              'var_cat': ['red', 'blue', 'red', 'green', 'green', 'blue']})

df['var_bool'] = df['var_bool'].astype('bool')
df['var_cat'] = df['var_cat'].astype(pd.api.types.CategoricalDtype())
Run Code Online (Sandbox Code Playgroud)

我想应用一个函数来计算唯一标签的频率,其权重由“权重”列给出:

df['var_bool'].value_counts() #I need to consider the weight of each row
df['var_cat'].value_counts() #I need to consider the weight of each row
Run Code Online (Sandbox Code Playgroud)

该函数必须适用于“var_bool”和“var_cat”,可能需要快速引擎(数据帧相当大)。多谢!

编辑:结果应该是:

#for "var_bool"
True 79
False 151

#for "var_cat"
red 90
blue 20
green 120
Run Code Online (Sandbox Code Playgroud)

python pandas pandas-groupby

4
推荐指数
1
解决办法
3118
查看次数

从状态中删除键(ReduxReducer)

我在 Redux 中有一个状态,当前呈现如下:

点击之前:

{0: {
    open: false,
    negation: false,
    close: false
    },
1: {
    open: false,
    negation: false,
    close: false,
    bool: "and"
    }
}
Run Code Online (Sandbox Code Playgroud)

点击后:

{0: {
    open: false,
    negation: false,
    close: false
    },
1: {}
}
Run Code Online (Sandbox Code Playgroud)

我想完全删除密钥 1 (一般来说它是 [action.id])。

目前减速器中的情况有:

case 'HANDLE_INCREASE_CHANGE':
  return {
    ...state,
    index: state.index + 1,
    [state.index + 1]: {
      open:false,
      negation: false,
      close: false,
      bool: 'and'
    }
  }
case 'HANDLE_DECREASE_CHANGE':
  return {
    ...state,
    index: state.index - 1,
    [state.index]: {}
  }
Run Code Online (Sandbox Code Playgroud)

错误的部分是:

[state.index]: {} …
Run Code Online (Sandbox Code Playgroud)

javascript redux

4
推荐指数
1
解决办法
7133
查看次数

React-select 警告隐藏至不受控制

我在代码中使用反应选择:

import React, {Component} from 'react';
import Select, {createFilter} from 'react-select';

let _ = require('underscore')

class Test extends Component {

  constructor(props) {
    super(props);
    this.state = {
      variables_api: [],
      selected_question_obj: null
    };
    this.handleChange_question = this.handleChange_question.bind(this)
  }

  componentDidMount() {
    fetch('http://127.0.0.1:5000/variables')
    .then(res => {
      return res.json()})
    .then(data => {
      this.setState({
        variables_api: data
      });
    })
  }

  handleChange_question(evt) {
    this.setState({
      selected_question_obj: evt
    });
  }

  render () {
    var key_api = this.state.variables_api.map(obj => {
      return {
        key_api: obj['index'],
        question_api: obj['Label Variabile'],
      };
    })
    var key = …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-select

3
推荐指数
1
解决办法
3458
查看次数

渲染一个 React 组件 n 次

我有以下代码:

import React, { Component } from 'react';
import {Button} from "@material-ui/core";
import Selector from "./Selector"

class Trigger extends Component {

  constructor(props) {
    super(props);
    this.state = {
      clicks: 0
    };
  }

  IncrementItem = () => {
    this.setState({
      clicks: this.state.clicks + 1
    });
  }

  DecreaseItem = () => {
    this.setState({
      clicks: this.state.clicks - 1
    });
  }

  render() {
    console.log(this.state)
    return (
      <div>

        <Button
        onClick={this.IncrementItem}
        variant="contained"
        color='primary'>
        add
        </Button>

        {this.state.clicks ?
          <Button
          onClick={this.DecreaseItem}
          variant="contained"
          color='primary'>
          remove
          </Button>:
          null}

        {this.state.clicks ?
          <Selector>
          </Selector>: …
Run Code Online (Sandbox Code Playgroud)

javascript arrays reactjs material-ui

2
推荐指数
1
解决办法
3249
查看次数

过滤嵌套数组 &gt;=

我有一个这样的数组:

const rawdata = [
  {
    top_feature_col: "s9",
    top_feature_row: 1,
    data: [{ cluster: 0, value: 151508, opportunity_perc: 69 }]
  },
  {
    top_feature_col: "s9",
    top_feature_row: 2,
    data: [{ cluster: 0, value: 127518, opportunity_perc: 70 }]
  },
  {
    top_feature_col: "s9",
    top_feature_row: 2,
    data: [{ cluster: 1, value: 12668, opportunity_perc: 40 }]
  }
];
Run Code Online (Sandbox Code Playgroud)

我想过滤哪里,opportunity_perc >= 50但我不知道该怎么做。

结果应该是:

const result = [
  {
    top_feature_col: "s9",
    top_feature_row: 1,
    data: [{ cluster: 0, value: 151508, opportunity_perc: 69 }]
  },
  {
    top_feature_col: "s9",
    top_feature_row: …
Run Code Online (Sandbox Code Playgroud)

javascript arrays

1
推荐指数
1
解决办法
49
查看次数