人々があなたの質問に答えるのを助けるために、データのサンプル セットを作成する必要があります。
import pandas as pd
import numpy as np
d = {'Age' : [36, 73, 30, 49, 47],
'Gender' : [0, 1, 0, 1, 1],
'Impressions' : [3, 3, 3, 3, 11],
'Clicks' : [0, 0, 0, 0, 0],
'Signed_In' : [1, 1, 1, 1, 1]}
df = pd.DataFrame(d)
手動で問題を作成する代わりに、人々が簡単にコピーして貼り付けることができるようにします。
numpy の round 関数は、負の小数点以下を丸めます:
df['Age_rounded'] = np.round(df['Age'], -1)
Age Clicks Gender Impressions Signed_In Age_rounded
0 36 0 0 3 1 40
1 73 0 1 3 1 70
2 30 0 0 3 1 30
3 49 0 1 3 1 50
4 47 0 1 11 1 50
次に、それらの値に辞書をマップできます。
categories_dict = {30 : 'Between 25 and 35',
40 : 'Between 35 and 45',
50 : 'Between 45 and 55',
70 : 'Between 65 and 75'}
df['category'] = df['Age_rounded'].map(categories_dict)
Age Clicks Gender Impressions Signed_In Age_rounded category
0 36 0 0 3 1 40 Between 35 and 45
1 73 0 1 3 1 70 Between 65 and 75
2 30 0 0 3 1 30 Between 25 and 35
3 49 0 1 3 1 50 Between 45 and 55
4 47 0 1 11 1 50 Between 45 and 55