pd.DataFrame({'email':["a@gmail.com", "b@gmail.com", "c@gmail.com", "d@gmail.com", "e@gmail.com",],
'one':[88, 99, 11, 44, 33],
'two': [80, 80, 85, 80, 70],
'three': [50, 60, 70, 80, 20]})
鉴于此 DataFrame,我想为每一列(一、二和三)计算特定范围内的值的数量。
范围例如:0-70、71-80、81-90、91-100
所以结果是:
out = pd.DataFrame({'colname': ["one", "two", "three"],
'b0to70': [3, 1, 4],
'b71to80': [0, 3, 1],
'b81to90': [1, 1, 0],
'b91to100': [1, 0, 0]})
执行此操作的惯用方法是什么?
最佳答案
这样就可以了:
out = pd.DataFrame()
for name in ['one','two','three']:
out[name] = pd.cut(df[name], bins=[0,70,80,90,100]).value_counts()
out.sort_index(inplace=True)
返回:
one two three
(0, 70] 3 1 4
(70, 80] 0 3 1
(80, 90] 1 1 0
(90, 100] 1 0 0
关于python - 计算 Pandas 中不均匀的箱子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47745373/