Pandas Categorical data type not behaving as expected
问题 I have the Pandas (version 0.15.2) dataframe below. I want to make the code column an ordered variable of type Categorical after the df creation as below. import pandas as pd df = pd.DataFrame({'id' : range(1,9), 'code' : ['one', 'one', 'two', 'three', 'two', 'three', 'one', 'two'], 'amount' : np.random.randn(8)}, columns= ['id','code','amount']) df.code = df.code.astype('category') >> 0 one >> 1 one >> 2 two >> 3 three >> 4 two >> 5 three >> 6 one >> 7 two >> Name: code, dtype: category >>