Skip to content

Instantly share code, notes, and snippets.

@nithyadurai87
Created June 12, 2021 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nithyadurai87/0e0a35168dd7d8f313e8a20161706aac to your computer and use it in GitHub Desktop.
Save nithyadurai87/0e0a35168dd7d8f313e8a20161706aac to your computer and use it in GitHub Desktop.
import pandas as pd
d = {'Names' : pd.Series(['Mahesh','Yazhini','Kadiresan','Malathi','Kumar','Sujith']),
'Gender' : pd.Series(['Male','Trans','Male','Female','Male','Trans'],dtype="category")}
df = pd.DataFrame(d)
print (df['Names'])
print (df['Gender'])
print (df['Gender'].cat.remove_categories(['Trans'])) # add_categories()
print (df['Gender'].cat.categories)
c = pd.Categorical(['AB+', 'B+', 'AB+', 'O+', 'B-', 'AB-','B+'])
df['Blood_group'] = pd.Series(c)
print (df['Blood_group'])
c = pd.Categorical(['AB+', 'B+', 'AB+', 'O+', 'B-', 'AB-','B+'],['O+','B+'])
df['Blood_group'] = pd.Series(c)
print (df['Blood_group'])
print (c.ordered)
df['Mid Year points'] = pd.Series(["50","90","80","50","120","100"])
df['Year End points'] = pd.Series(["80","50","80","90","100","50"])
df['Mid Year points'] = df['Mid Year points'].astype("category", categories=["50","80","90"], ordered=True)
df['Year End points'] = df['Year End points'].astype("category", categories=["50","80","90"], ordered=True)
print (df['Mid Year points']>df['Year End points'])
print (df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment