我有以下数据框:
List<RadioButton> radioButtonList = new ArrayList<>();
private int selectedPosition = -1;
@Override
public void onBindItemViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final ItemViewHolder iHolder = (ItemViewHolder)holder;
final RestaurauntMenuItemDetail r = list.get(position);
iHolder.radioButton.setTag(position);
iHolder.radioButton.setOnCheckedChangeListener(null);
iHolder.radioButton.setChecked(r.getSelected());
iHolder.radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer pos = (Integer) iHolder.radioButton.getTag();
RadioButton rb = (RadioButton) v;
if(radioButtonList.isEmpty()){
radioButtonList.add(rb);
rb.setChecked(true);
list.get(pos).setSelected(true);
selectedPosition = pos;
}else{
RadioButton nrb = radioButtonList.get(0);
if(nrb.isChecked()){
Log.i("NEW RB BUTTON WAS CHECKED", "CHECKED NRB");
nrb.setChecked(false);
list.get(selectedPosition).setSelected(false);
}
radioButtonList.remove(0);
radioButtonList.add(rb);
rb.setChecked(true);
list.get(pos).setSelected(true);
selectedPosition = pos;
}
}
});
我想获取成本值总和小于40的前 n 行。因此,在上述情况下,我想创建一个仅前2行的新数据框。 有没有一种有效的方法可以在数据帧中做到这一点而无需遍历每一行?
答案 0 :(得分:1)
您可以使用pandas.DataFrame.cumsum()
来返回DataFrame或Series元素的累积总和:
df = pd.DataFrame({'Cost': [12, 20, 21], 'Country': [34, 34, 34]})
cumSumOfCost = df['Cost'].cumsum()
mask_cumSumBelow40 = cumSumOfCost < 40
df_sumBelow40 = df.loc[mask_sumBelow40]
答案 1 :(得分:0)
您可以使用cumsum()来获取成本值总和小于40的前n行。
df [df ['Cost']。cumsum()<40]