在一对多的大熊猫中分配价值

时间:2019-01-30 19:52:12

标签: python python-3.x pandas dataframe

我是Pandas的新手,我试图将两个数据框以多对一的方式合并。我不想获得与“一个”键相关的重复值,而是想以某种方式分配它,即:如果“许多”键重复了4次,我希望将值关联到“一个”键除以4,不会重复四次。

所以我有:

df_many = pd.DataFrame([['23040010', '230400'], ['23040020', '230400'], ['23040030', '230400'], ['23040040', '230400']], columns=['A', 'B']) 
df_one = pd.DataFrame([['230400', 40000]], columns = ['B', 'C'])

print (df_many)
print (df_one)

   A       B
0  23040010  230400
1  23040020  230400
2  23040030  230400
3  23040040  230400

   B      C
0  230400  40000

pd.merge(df_many, df_one, how='left', on='B')

    A           B       C
0   23040010    230400  40000
1   23040020    230400  40000
2   23040030    230400  40000
3   23040040    230400  40000

我正在寻找的是:

    A           B       C
0   23040010    230400  10000
1   23040020    230400  10000
2   23040030    230400  10000
3   23040040    230400  10000

有什么想法吗?在此先感谢!

2 个答案:

答案 0 :(得分:1)

我认为您最好的选择是按照每个小组的规模创建一个系列,然后将其除以:

df_merged = pd.merge(df_many, df_one, how='left', on='B')
group_sizes = df_merged.groupby(["B", "C"])["B"].transform("size")
df_merged["C"] = df_merged["C"] / group_sizes 

结果:

          A       B        C
0  23040010  230400  10000.0
1  23040020  230400  10000.0
2  23040030  230400  10000.0
3  23040040  230400  10000.0

答案 1 :(得分:0)

您也可以单行执行:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter{
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<Pietanze>> expandableListDetail;
private int idbhub;
private Context classe;

private ArrayList<Integer> count = new ArrayList<>(100);

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,HashMap<String, List<Pietanze>> expandableListDetail,int idbhub, Context classe)
{
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
    this.idbhub=idbhub;
    this.classe=classe;
}

@Override
public int getGroupCount()
{
    return this.expandableListTitle.size();
}

@Override
public int getChildrenCount(int listPosition)
{
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}

@Override
public Object getGroup(int listPosition)
{
    return this.expandableListTitle.get(listPosition);
}

@Override
public Object getChild(int listPosition, int expandedListPosition)
{
  return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).get(expandedListPosition).getNome()+" "+this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).get(expandedListPosition).getPrezzo().toString()+" €";
}

@Override
public long getGroupId(int listPosition)
{
    return listPosition;
}

@Override
public long getChildId(int listPosition, int expandedListPosition)
{
    return expandedListPosition;
}

@Override
public boolean hasStableIds()
{
    return false;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}


@Override
public View getChildView(final int listPosition, final int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);

    if (convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText);

    expandedListTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            AlertDialog.Builder alertingredienti = new AlertDialog.Builder(classe);
            alertingredienti.setMessage(expandableListDetail.get(expandableListTitle.get(listPosition)).get(expandedListPosition).getIngredienti()).setTitle("LISTA INGREDIENTI").create();
            alertingredienti.show();
        }
    });

    Button btadd = (Button) convertView.findViewById(R.id.add);
    Button btsub = (Button) convertView.findViewById(R.id.sub);
    final TextView contatore = (TextView) convertView.findViewById(R.id.contatore);

    if(idbhub==1)
    {

        btadd.setText("ADD");
        btsub.setText("SUB");

        btadd.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
               contatore.setText("1");

            }
        });

        btsub.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

            }
        });
    }
    else
    {
        btadd.setVisibility(View.GONE);
        btsub.setVisibility(View.GONE);
        contatore.setVisibility(View.GONE);
    }
    return convertView;
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition)
{
    return true;
}
相关问题