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
0       1     Monday
1       1     Monday
2       2    Tuesday
3       3  Wednesday
4       4   Thursday
5       7        NaN
6       8        NaN
7       9        NaN
8       0     Sunday

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