FUNCTIONS IN PANDAS
In this tutorial, we will learn about different types of functions in Pandas that will help us to understand and use pandas more efficiently for solving different types of tasks.
Understanding Functions in Pandas
By now we know how to create different types of data structures in Pandas. We have learned about creating a Series and a DataFrame. Now it’s time to learn about different functionalities in Pandas to perform different tasks.
A few functionalities are:
Functions | Description |
dtypes | It returns the type of data |
empty | Checks whether the Dataframe is empty or not. If yes, then it turns True. |
ndim | Returns the number of dimensions of the dataframe. |
size | Returns the size of the data structure |
head() | Returns rows of the data that you specify inside the parentheses from the beginning. |
tail() | Returns rows of the data that you specify inside the parentheses from the last.. |
Transpose | Converts rows into columns and columns into rows |
We will be looking at these functions examples one by one to understand more about them:
Functions in Pandas: dtypes
It returns the type of data
df.dtypes
Output:
Persons object Jobs object Dtype: object
Functions in Pandas: empty
Checks whether the Dataframe is empty or not. If yes, then it turns True.
df.empty
Output:
False
Since our dataframe is not empty hence empty returned False.
Functions in Pandas: ndim
Returns the number of dimensions of the dataframe.
df.ndim
Output:
2
Functions in Pandas: size
Returns the size of the data structure (number of rows and columns):
df.size
Output:
8
head()
Returns rows of the data that you specify inside the parentheses from the beginning.
df.head(2)
Output:
Persons Jobs First Hira Entrepreneur Second Sanjeev Doctor
tail()
Returns rows of the data that you specify inside the parentheses
df.tail(1)
Output:
Persons Jobs Fourth Ali Chef
axes
axes function returns the rows axis lable and column axis label. Let’s look at a quick example:
import pandas as pd # intialise data of lists. data = {'Name':['Hira', 'Sanjeev', 'Rahul', 'Ali'], 'Occupation':['Entrepreneur', 'Doctor', 'Actor', 'Chef'], 'Salary':[30000, 40000, 25000, 32000], 'Age':[25,24,27,29]} # Create DataFrame df = pd.DataFrame(data) # Print the output. print(df) df.axes
Output:
Above, you can see that we are able to create axis labels of rows and columns by simply using the axes function. It is displaying the range index as well as a separated index from the dictionary keys.
Transpose
Converts rows into columns and columns into rows
df.T
Output:
First Second Third Fourth Persons Hira Sanjeev Rahul Ali Jobs Entrepreneur Doctor Actor Chef
Rows are converted into columns
To learn more about other general functions present in pandas, check out the latest documentation here