1
私は一連の数値を持っており、データフレームのすべてのバケットに含まれる数値の割合を知りたいと思います。定義済みのバケットに基づいた割合のビン%
df['cuts']
は、値として10,20および50を有する。具体的には、series
の%が[0-10], (10-20] and (20-50]
binにあり、これは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)