Loading, please wait...

MCQ

What are the basic Functionality of Pandas Data Structure| Pandas tutorial

This article is brief explanations of the basic functionality of pandas data structure Series and DataFrame.

Basic Functionality Of Pandas Data Structure

Before diving into this article, I would suggest reading my previous articles on the Pandas data structure. Through this article,
You will learn the following thing about the basic functionalities of the Pandas data structure in brief.

1. Series Basic Functionality:
axes: It returns a list of the row axis labels.
dtype: It returns the data type.
empty: If the series is empty, it returns True.
ndim: It returns the number of dimensions of the data.
size: It returns the number of elements.
values: It returns the series as ndarray.
head(): It returns first ‘m’ rows.
tail(): It returns last ‘m’ rows.

Example:

import pandas as pd
import numpy as np
x= pd.Series(np.random.randn(3))
print(x)
#output:
0 -0.772163
1 0.829784
2 -0.016395
dtype: float64

#axes
print(f"The axes are: {x.axes}")
#output:
#The axes are: [RangeIndex(start=0, stop=3, step=1)]

#dtype
print(f"data type of the object: {x.dtype}")
#output:
#data type of the object: float64

#empty
print(f" object is empty ?: {x.empty}")
#output:
#object is empty ?: False

#ndim
print(f"The dimension of the object: {x.ndim}")
#output:
#The dimension of the object: 1

#size
print(f"The size of the object: {x.size}")
#output:
The size of the object: 3

#values
print(f"The data series: {x.values}")
#output:
#The data series: [-0.77216261 0.82978381 -0.01639488]

#head()
print(f"first 3 rows of the data series: {x.head(3)}")
#output:
#first 3 rows of the data series: 0 -0.772163
1 0.829784
2 -0.016395
dtype: float64

#tail()
print(f"last 3 rows of the data series: {x.tail(3)}")
#output:
#last 3 rows of the data series: 0 -0.772163
1 0.829784
2 -0.016395
dtype: float64


2. DataFrame Basic Functionality

Now, let’s understand what are the basics functionality of DataFrame. Important methods and attributes of that help in Dataframe Basic functionality are listed below:

axes: It returns a list of rows and columns labels as the only members.
dtypes: It returns the data type of the object.
empty: It returns True either if NDframe is empty or any axis is of the length 0.
ndim: It returns dimensions of the number of axes and ndarray.
shape: It returns a tuple representing the dimension of the dataframe.
values: It returns the actual data of the Dataframe.
head(): It returns the first n rows of the Dataframe, by default it returns the first 5 rows, we can pass custom number to it.
tail(): It returns the last n rows of the Dataframe, by default it returns the last 5 rows, we can pass custom number to it.
T: It transposes the rows and columns.
size: It returns the number of elements.

Example:

Now, let’s create a Dataframe with a dictionary and see all the above attributes and methods operate.

import pandas as pd
#create dictionary of Series

x= {'Fruits_Name': pd.Series(['Apple', 'Banana', 'Orange','Mango','Pear']),
'Weight':pd.Series([25,56,89,78,80]),
'PricePerKg':pd.Series([109,80,98,112,130])}

#create Dataframe
df = pd.DataFrame(x)

print(df)
#output:
'''
Fruits_Name Weight PricePerKg
0 Apple 25 109
1 Banana 56 80
2 Orange 89 98
3 Mango 78 112
4 Pear 80 130'''

print(f"The axes are:\n {df.axes}")
#output:
#The axes are: [RangeIndex(start=0, stop=5, step=1), Index(['Fruits_Name', 'Weight', 'PricePerKg'], dtype='object')]


print(f"data type of the object:\n {df.dtypes}")
#output:
#data type of the object:
Fruits_Name object
Weight int64
PricePerKg int64
dtype: object


print(f" object is empty ?:\n {df.empty}")
#output:
object is empty ?:
False


print(f"The dimension of the object:\n {df.ndim}")
#output:
#The dimension of the object:2

print(f"The size of the object:\n {df.size}")
#output:
#The size of the object:15

print(f"The data series: \n {df.values}")
#output:
#The data series:
[['Apple' 25 109]
['Banana' 56 80]
['Orange' 89 98]
['Mango' 78 112]
['Pear' 80 130]]

print(f"first 3 rows of the data series:\n {df.head(3)}")
#output:
#first 3 rows of the data series:
Fruits_Name Weight PricePerKg
0 Apple 25 109
1 Banana 56 80
2 Orange 89 98

print(f"last 3 rows of the data series:\n {df.tail(3)}")
#output:
last 3 rows of the data series:
Fruits_Name Weight PricePerKg
2 Orange 89 98
3 Mango 78 112
4 Pear 80 130


print(f"Transpose rows and columns:\n {df.T}")
#output:
Transpose rows and columns:
0 1 2 3 4
Fruits_Name Apple Banana Orange Mango Pear
Weight 25 56 89 78 80
PricePerKg 109 80 98 112 130
MCQ

Related Article