Pandas Series cat function as_unordered() in Python
The Pandas Series cat function as_unordered() used to set the Categorical to be unordered. It returns a unordered Categorical.
Syntax
 1 Series.cat.as_unordered(*args, **kwargs)
*args : It specifies the additional arguments to be passed to the function.
**kwards : It specifies the additional keywords to be passed to the function.
as_unordered() function
 1 import pandas as pd
 2 
 3 ser = pd.Series(['y', 'w', 'x', 'x', 'z'], dtype= 'category')
 4 
 5 print('The original categories object :')
 6 print(ser)
 7 
 8 res = ser.cat.as_unordered()
 9 print('The ordered categories :')
 10 print(res)
In the above example, a Categorical object is created by passing an initial values. A as_unordered() is called that keep elements unordered as shown below.
Output
 1 The original categories object :
 2 0    y
 3 1    w
 4 2    x
 5 3    x
 6 4    z
 7 dtype: category
 8 Categories (4, object): ['w', 'x', 'y', 'z']
 9 The ordered categories :
 10 0    y
 11 1    w
 12 2    x
 13 3    x
 14 4    z
 15 dtype: category
 16 Categories (4, object): ['w', 'x', 'y', 'z']

Example 2

Example 2
 1 import pandas as pd
 2 
 3 cat = pd.Categorical(['x1', 'x1', 'y1', 'x1', 'z1'])
 4 
 5 print('The original categories object :')
 6 print(cat)
 7 
 8 res = cat.as_unordered()
 9 print('The ordered categories :')
 10 print(res)
In the above example, a Categorical object is created by passing an initial values. A as_unordered() is called that keep elements unordered as shown below.
Output
 1 The original categories object :
 2 ['x1', 'x1', 'y1', 'x1', 'z1']
 3 Categories (3, object): ['x1', 'y1', 'z1']
 4 
 5 The ordered categories :
 6 ['x1', 'x1', 'y1', 'x1', 'z1']
 7 Categories (3, object): ['x1', 'y1', 'z1']
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us