基于预定义存储桶的百分比箱

时间:2017-07-27 23:24:42

标签: python-2.7 python-3.x pandas dataframe data-manipulation

我有一系列数字,我想知道数据帧的每个桶中有多少数字。

df['cuts']的值为10,20和50。具体来说,我想series bin中[0-10], (10-20] and (20-50]%的百分比,这应该附加到df数据框。

我写了以下代码。我绝对觉得它可以是即兴的。任何帮助表示赞赏。

bin_cuts = [-1] + list(df['cuts'].values)
out = pd.cut(series, bins = bin_cuts)
df_pct_bins = pd.value_counts(out, normalize= True).reset_index()
df_pct_bins = pd.concat([df_pct_bins['index'].str.split(', ', expand = True), df_pct_bins['cuts']], axis = 1)
df_pct_bins[1] = df_pct_bins[1].str[:-1].astype(str)
df['cuts'] = df['cuts'].astype(str)
df_pct_bins = pd.merge(df, df_pct_bins, left_on= 'cuts', right_on= 1)

1 个答案:

答案 0 :(得分:1)

考虑示例数据package com.example.marcusgrant.test12345; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class Console_Activity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.console_login); final ImageView xShowDialog = (ImageView) findViewById(R.id.XboxImBtn); ImageView pShowDialog = (ImageView) findViewById(R.id.PsnImBtn); xShowDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder mBuilder = new AlertDialog.Builder(Console_Activity.this); View mView = getLayoutInflater(). inflate(R.layout.xdialog_signin, null); final EditText XEmail = (EditText) mView.findViewById(R.id.etEmail); final EditText Xpassword = (EditText) mView.findViewById(R.id.etPassword); Button XLogbtn = (Button) mView.findViewById(R.id.Logbtn); Button XNtnbtn = (Button) mView.findViewById(R.id.ntnbtn); XLogbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!XEmail.getText().toString().isEmpty() && !Xpassword.getText().toString().isEmpty()) { Toast.makeText(Console_Activity.this, R.string.successful_login, Toast.LENGTH_SHORT).show(); xShowDialog.setImageResource(R.drawable.xboxbuttongreen); //cannot resolve symbol 'alertdialog' dialog.dismiss(); } else { Toast.makeText(Console_Activity.this, R.string.error_login_message, Toast.LENGTH_SHORT).show(); } } }); XNtnbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //cannot resolve symbol 'alertdialog' alertDialog.dismiss(); } }); mBuilder.setView(mView); AlertDialog dialog = mBuilder.create(); dialog.show(); } }); } } df

s

选项1
df = pd.DataFrame(dict(cuts=[10, 20, 50])) s = pd.Series(np.random.randint(50, size=1000))

np.searchsorted

选项2
c = df.cuts.values df.assign( pct=df.cuts.map( pd.value_counts( c[np.searchsorted(c, s)], normalize=True ))) cuts pct 0 10 0.216 1 20 0.206 2 50 0.578

pd.cut