Skip to content Skip to sidebar Skip to footer

Map Pandas Values To A Categorical Level

New to pandas. R users use the split, apply, combine pattern for analyzing sub-populations. e.g. gender, 1='Male', 2='Female', 9='Unknown. I have a dataframe with a day column

Solution 1:

Not sure I'm understanding what you are trying to do... Maybe you are overcomplicating something easy: is this what you want? If not please provide a clear example with input and expected output.

data = pd.DataFrame({'values': [1, 1, 2, 3, 4, 7, 8, 9, 0]})
dow = {
    0:"Sunday",
    1:"Monday",
    2:"Tuesday",
    3:"Wednesday", 
    4:"Thursday", 
    5:"Friday", 
    6:"Saturday"
}
data["dow"] = data['values'].map(dow)

print data

result:

   values        dow
01     Monday
11     Monday
22    Tuesday
33  Wednesday
44   Thursday
57NaN68NaN79NaN80     Sunday

Post a Comment for "Map Pandas Values To A Categorical Level"