Instead of using a map, you can use pd.cut (thanks to DSM and Jeff for pointing this out):
import numpy as np
import pandas as pd
mn = pd.DataFrame(
{'fld1': [2.23, 4.45, 7.87, 9.02, 8.85, 3.32, 5.55],
'fld2': [125000, 350000, 700000, 800000, 200000, 600000, 500000],
'lType': ['typ1', 'typ2', 'typ3', 'typ1', 'typ3', 'typ1', 'typ2'],
'counter': [100, 200, 300, 400, 500, 600, 700]})
result = mn.groupby(
[pd.cut(mn['fld1'], [1,4,6,9,99], labels=['tag1', 'tag2', 'tag3', 'tag4']),
pd.cut(mn['fld2'], [100000, 150000, 650000, 5000000],
labels=['100-150', '150-650', '650-5M']),
'lType']).sum()
print(result)
gives
counter fld1 fld2
lType
tag1 100-150 typ1 100 2.23 125000
150-650 typ1 600 3.32 600000
tag2 150-650 typ2 900 10.00 850000
tag3 150-650 typ3 500 8.85 200000
650-5M typ3 300 7.87 700000
tag4 650-5M typ1 400 9.02 800000
, getTag getTag1 . pd.cut np.searchsorted, ( , searchsorted O (log n) , C O (n), Python).
: , sliceDef.keys(), - . ( , Python3). :
if ((float(rangeAttribute) >= float(bounds[0]))
and (float(rangeAttribute) <= float(bounds[1]))):
, , rangeAttribute bounds.
, .
pd.cut , , .
: , - ( Andy Hayden):
import numpy as np
import pandas as pd
def getTag(rangeAttribute, sliceDef):
for sl in sliceDef.keys():
bounds = sliceDef[sl]
if ((float(rangeAttribute) >= float(bounds[0]))
and (float(rangeAttribute) <= float(bounds[1]))):
return sl
sliceDef = {'tag1': [1, 4], 'tag2': [4, 6], 'tag3': [6, 9],
'tag4': [9, 99]}
sliceDef1 = {'100-150': [100000, 150000],
'150-650': [150000, 650000],
'650-5M': [650000, 5000000]}
mn = pd.DataFrame(
{'fld1': [2.23, 4.45, 7.87, 9.02, 8.85, 3.32, 5.55],
'fld2': [125000, 350000, 700000, 800000, 200000, 600000, 500000],
'lType': ['typ1', 'typ2', 'typ3', 'typ1', 'typ3', 'typ1', 'typ2'],
'counter': [100, 200, 300, 400, 500, 600, 700]})
result = mn.groupby([mn['fld1'].apply(getTag, args=(sliceDef, ))
,mn['fld2'].apply(getTag, args=(sliceDef1, )),
'lType'] ).sum()
print(result)
, apply , pd.cut , . , apply , .