将numpy数组附加到多索引数据框

时间:2018-05-14 07:57:21

标签: python pandas numpy

我在填充大熊猫DataFrame时遇到了一些麻烦。我按照发现here的说明生成MultiIndex DataFrame。该示例正常工作,除了我想要一个数组而不是一个值。

activity = 'Open_Truck'
id = 1
index = pd.MultiIndex.from_tuples([(activity, id)], names=['activity', 'id'])
v = pd.Series(np.random.randn(1, 5), index=index)
  

例外:数据必须是1维的

如果我将randn(1, 5)替换为randn(1),则可以正常使用。对于randn(1, 1),我应该使用randn(1, 1).flatten('F')但也可以使用v = pd.Series(np.random.randn(1, 5).flatten('F'), index=index) 。 尝试时:

np.array
  

ValueError:传递的项目数量错误5,展示位置意味着1

我的目的是为每行中的np.random.randnactivity添加1个特征向量(当然,它们在实际案例中为id,而不是MultiIndex)。
那么,我如何设法在DataFrame Series

中添加数组

编辑:
由于我是大熊猫的新手,我将DataFrameDataFrame混合在一起。我可以使用默认为二维的arrays = [np.array(['Open_Truck']*2), np.array(['1', '2'])] df = pd.DataFrame(np.random.randn(2, 4), index=arrays) df 0 1 2 3 Open 1 -0.210923 0.184874 -0.060210 0.301924 2 0.773249 0.175522 -0.408625 -0.331581 来实现上述目的:

class Dashboard extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      // ...
      serviceHTTPSCa: "",
      serviceHTTPSCaError: ""
    }

    // ...

    this.handleChangeServiceHTTPSCa = this.handleChangeServiceHTTPSCa.bind(this);
  }

  handleChangeServiceHTTPSCa(event) {
    if (event.target.value.match(/^([a-z0-9_-]+).crt$/i)) {
      this.setState({
        serviceHTTPSCa: event.target.value,
        serviceHTTPSCaError: "",
      });
    } else {
      this.setState({
        serviceHTTPSCaError: "Invalid format!"
      });
    }
  }

  render() {
    return (
      <div>
        {/* some other markup */}
        <TextField
          hintText="Service https Ca..."
          fullWidth={true}
          value={this.state.serviceHTTPSCa}
          onChange={this.handleChangeServiceHTTPSCa}
          errorText={this.serviceHTTPSCaError}
          type="text"
        />
        {/* some other markup */}
      </div>
    );
  }
}

1 个答案:

答案 0 :(得分:1)

有问题class ViewController: UIViewController { let views: [UIView] = [ { let view = UIView(); view.backgroundColor = .red; return view; }(), { let view = UIView(); view.backgroundColor = .green; return view; }(), { let view = UIView(); view.backgroundColor = .blue; return view; }() ] lazy var stackView: UIStackView = { let stackView = UIStackView(frame: CGRect(x: 50.0, y: 100.0, width: 300.0, height: 200.0)) self.view.addSubview(stackView) stackView.backgroundColor = .black stackView.distribution = .fillEqually return stackView }() override func viewDidLoad() { super.viewDidLoad() stackView.addArrangedSubview(views[0]) stackView.addArrangedSubview(views[1]) stackView.addArrangedSubview(views[2]) } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) UIView.animate(withDuration: 0.3) { if self.stackView.arrangedSubviews.count == 3 { self.views[1].alpha = 0.0 self.stackView.removeArrangedSubview(self.views[1]) } else { self.views[1].alpha = 1.0 self.stackView.insertArrangedSubview(self.views[1], at: 1) } self.stackView.layoutIfNeeded() } } } 只有一个元组,数据长度不同,MultiIndex所以长度不匹配:

5

在第一个aproach长度相同,activity = 'Open_Truck' id = 1 #get 5 times tuples index = pd.MultiIndex.from_tuples([(activity, id)] * 5, names=['activity', 'id']) print (index) MultiIndex(levels=[['Open_Truck'], [1]], labels=[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], names=['activity', 'id']) print (len(index)) 5 v = pd.Series(np.random.randn(1, 5).flatten('F'), index=index) print (v) activity id Open_Truck 1 -1.348832 1 -0.706780 1 0.242352 1 0.224271 1 1.112608 dtype: float64 ,因为列表中有一个元组:

1