2021年3月8日 星期一

[ Python 常見問題 ] Pandas - Binning column with python pandas

 Source From Here

Question
I have a DataFrame column with numeric values:
  1. import pandas as pd  
  2.   
  3. df = pd.DataFrame([[46.5], [44.2], [100.0], [42.12]], columns=['percentage'])  
I want to see the column as bin counts:
  1. bins = [015102550100]  
How can I get the result as bins with their value counts?

HowTo
You can leverage API pandas.cut to bin the target column:


Then you can leverage another API DataFrame.groupby to calculate the count:


Or:
  1. df['binned'].value_counts()  
Output:
  1. 25-50     3  
  2. 50-100    1  
  3. 10-25     0  
  4. 5-10      0  
  5. 1-5       0  
  6. 0-10      0  
  7. Name: binned, dtype: int64  





沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...