DEV Community

Cover image for Usefull functions in python pandas 🐼
Ajith R
Ajith R

Posted on

Usefull functions in python pandas 🐼

*****head(n) and tail(n)*
The head🧠 and tail function is used to give you a quick look at the dataframe by printing some of its values.
By default, the head function shows the first 5 rows, and tail() shows the last 5 rows of the DataFrame.
The n in the arguments is the total number of rows you want to print.
df = pd.read_csv('Elections.csv') # displaying first 5 rows print (df.head())

df.isnull().sum()
isnull().sum() returns how mach NULL values are present in a columns.
df.isnull().sum()

df.count(0)
It returns the number of data in the DataFrame in the specified direction. When the direction is 0, it provides the number of data in the columns.
print(df.count(0))

df['col'].value_counts()
We can get the value counts of each category using this function.
df['Year'].value_counts()

df.['columns'].unique()
It is used to find out the unique values of a categorical column.
df['Year'].unique()
array([2020, 2018, 2016, 2014, 2012, 2010, 2008, 2006, 2004, 2002, 2000, 1998, 1996, 1994, 1992])

df.nunique()
This function lets you know how many
unique values you have in a column. The great thing is, this function can be used on the total dataset as well to know the number of unique values in each column.
df.nunique()
#or df['Year'].nunique()

df.iloc[rows, columns]
This function takes as a parameter the rows and column indices and gives you the subset of the DataFrame accordingly.
# 5 rows of 1 and 2 column, 3 is excluded
df.iloc[:5,1:3]

df.loc[rows, columns]
This function does almost the similar operation as .iloc() function. But here we can specify exactly which row index we want and also the name of the columns we want in our subset.

#returns 1, 2, 3 and 5th row of Year and
# Office column
df.loc[[1, 2, 3, 5], ['Year', 'Office']]

Top comments (0)