8 methods to get size of Pandas Series/DataFrame Object


Written by - Deepak Prasad

Introduction

Size refers to the number of elements/ object dimensions/ total value count of the given pandas object.

Series is a data structure used to store only single data type elements in an one dimensional format. DataFrame is a data structure used to store only multiple data type elements in an two dimensional format i.e rows and columns.

We can create Series using pandas.Series() function and DataFrame using pandas.DataFrame() function. There are many ways to return of the given pandas object. In this tutorial we will discuss how to get the size of the pandas objects from rows, columns and entire DataFrame.

 

Method 1 : Use pandas.Series.str.len() to get size of values in Series

In this method, we are going to return the character count in each data values from Series object using pandas.Series.str.len()

This function will return the length of values according to the index .

Syntax:

data.str.len()

where, data is the input Series

Example:

In this approach, we are going to create Series data for names with 4  and then apply the above method to return the length of each name according to the index

# importing pandas
import pandas

# create Series data
data = pandas.Series(["sai","sravan","abhishek","india"])

# display the data
print(data, "\n===================")

# function - Series.str.len()
print("Return the size of individual values from Panda Series: \n", data.str.len())

Output:

0 sai
1 sravan
2 abhishek
3 india
dtype: object 
===================
Return the size of individual values from Panda Series: 
0 3
1 6
2 8
3 5
dtype: int64

 

Method 2 : Use pandas.Series.size() to list total number of values in Series

In this method, we are going to return the total  values from Series object using pandas.Series.size(). This function will return the count of number of values  according.

Syntax:

data.size()

where, data is the input Series

Example:

In this approach, we are going to create Series data for names with 4  and then apply the above method to return the total number of values.

# importing pandas
import pandas

# create Series data
data = pandas.Series(["sai","sravan","abhishek","india"])

# display the data
print(data, "\n===================")

# function - Series.size
print("Number of values in Panda Series: \n", data.size)

Output:

0 sai
1 sravan
2 abhishek
3 india
dtype: object 
===================
Return the size of individual values from Panda Series: 
4

 

Method 3 : Get DataFrame object size using pandas.DataFrame.size()

In this method, we are going to return the total values from DataFrame object.

pandas.DataFrame.size() will return the count of number of values  (in all columns)according.

Syntax:

data.size()

where, data is the input DataFrame.

We can also get the size of data for specific column in the dataframe by providing specific column name.

Syntax:

data['column'].size()

column refers to the column name to return the size.

 

Example 1: Get entire Pandas DataFrame size

In this approach, we are going to create DataFrame with 3 columns and 4 rows  then apply the above method to return the total number of values.

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================")

# function - dataframe.size
print("Number of objects in Panda DataFrame: \n", data.size)

Output:

Name Age Marks
0 sai 21 89
1 sravan 22 87
2 deepak 31 89
3 prasad 34 90 
===================
Number of objects in Panda DataFrame:
12

 

Example 2: Get number of elements per column in DataFrame

In this approach, we are going to create DataFrame with 3 columns and 4 rows  then apply the above method to return the total number of values from all Columns separately.

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -Name  dataframe.size
print("Number of elements in Name column: ", data['Name'].size)

# function - Marks dataframe.size
print("Number of elements in Marks column: ", data['Marks'].size)

# function -Age dataframe.size
print("Number of elements in Age column: ", data['Age'].size)

Output:

       Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================

Number of elements in Name column: 4
Number of elements in Marks column: 4
Number of elements in Age column: 4

 

Method 4 : Get number of rows, columns, etc in detail with pandas.DataFrame.info()

info() is used to get the information from the pandas dataframe. It will return the  memory used by the dataframe, column names with date types and non-null value counts.

Syntax:

data.info()

where, data is the input dataframe

Example:

In this approach, we are going to create DataFrame with 3 columns and 4 rows  then apply the above method to return the information

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data)

# function -info()
print(data.info())

Output:

Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   Name    4 non-null      object
 1   Age     4 non-null      int64 
 2   Marks   4 non-null      int64 
dtypes: int64(2), object(1)
memory usage: 224.0+ bytes
None

 

Method 5 : pandas.DataFrame.shape

This method will return the number of rows and number of columns from the dataframe. It will return a tuple - first element refers row count and second element refers column count.

Syntax:

data.shape

where, data is the input dataframe

Example 1:

In this approach, we are going to create DataFrame with 3 columns and 4 rows  then apply the above method to return the dimensions

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -shape
print("Number of rows, columns: ", data.shape)

Output:

     Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================
Number of rows, columns: (4, 3)

 

Method 6 : pandas.DataFrame/Series.ndim

This method will return the number of dimensions from the dataframe. In dataframe there are two dimensions, one is row and another is column, so the result for the dataframe is 2. For series , it is 1.

Syntax:

data.shape

where, data is the input dataframe/series

 

Example 1: Get the dimension of Pandas DataFrame

In this approach, we are going to create DataFrame with 3 columns and 4 rows  then apply the above method to return the ndim

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -ndim
print("Get the DataFrame dimension: ", data.ndim)

Output:

     Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================

Get the DataFrame dimension: 2

 

Example 2: Get the dimension of Pandas Series

In this approach, we are going to create Series then apply the above method to return the shape

# importing pandas
import pandas

# create Series data
data = pandas.Series(["sai","sravan","deepak","prasad"])

# display the data
print(data, "\n===================\n")

# function - ndim
print("Get the Series dimension: ", data.ndim)

Output:

0       sai
1    sravan
2    deepak
3    prasad
dtype: object
===================
Get the Series dimension: 1

 

Method 7 : Get number of values from Series or DataFrame using len()

In this method, we are going to return the total values from Series/DataFrame object.

len() will return the count of number of values  according.

Syntax:

len(data)

where, data is the input Series/DataFrame

 

Example 1: Get number of elements in a Panda Series

In this approach, we are going to create Series data for names with 4 values and then apply the above method to return the total number of values.

# importing pandas
import pandas

# create Series data
data = pandas.Series(["sai","sravan","deepak","prasad"])

# display the data
print(data)

# function - len
print("Number of elements in Series: ", len(data))

Output:

0       sai
1    sravan
2    deepak
3    prasad
dtype: object 
===================

Number of elements in Series:  4

 

Example 2: Get number of rows in Pandas DataFrame

In this approach, we are going to create dataframe for names with 4 rows and then apply the above method to return the total number of values.

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -len
print("Number of rows in DataFrame: ", len(data))

Output:

     Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================

Number of rows in DataFrame: 4

 

Method 8 : Get number of columns in DataFrame using len(pandas.DataFrame.columns)

By using len(), we can return the number of columns from the dataframe with columns method specified inside len function.

Syntax:

len(data.columns)

where, data is the input dataframe

 

Example: In this approach, we are going to create dataframe for names with 4  and then apply the above method to return the total number of columns.

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -len
print("Number of columns in DataFrame: ", len(data.columns))

Output:

     Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================
Number of rows in DataFrame: 3

 

Method 9 : Get number of rows and columns in DataFrame using len(pandas.DataFrame.axes)

With len() function, we can also return the number of rows/columns using axes().

axes[0] - specifies row and axes[1] specifies the column

Syntax:

len(data.axes[0])
len(data.axes[1])

where, data is the input dataframe

 

Example 1: Get the number of columns in Pandas DataFrame

In this example, we are specifying column - in axes to get the column count

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -len
print("Number of columns in DataFrame: ", len(data.axes[1]))

Output:

     Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================
Number of columns in DataFrame: 3

 

Example 2: Get number of rows in Pandas DataFrame

In this example, we are specifying row - in axes to get the row count

# importing pandas
import pandas

# create dataframe
data = pandas.DataFrame({'Name':["sai","sravan","deepak","prasad"],
                         'Age':[21,22,31,34],
                         'Marks':[89,87,89,90]})

# display the dataframe
print(data, "\n===================\n")

# function -len
print("Number of rows in DataFrame: ", len(data.axes[0]))

Output:

     Name  Age  Marks
0     sai   21     89
1  sravan   22     87
2  deepak   31     89
3  prasad   34     90
===================
Number of rows in DataFrame: 4

 

Summary

In this tutorial, we discussed how to get the size of the pandas objects. We covered all the methods to get the size for the Series and the DataFrame.

  1. pandas.Series.str.len()
  2. pandas.Series.size()
  3. pandas.DataFrame.size()
  4. pandas.DataFrame.info()
  5. pandas.DataFrame.shape
  6. pandas.DataFrame/Series.ndim
  7. len()
  8. len(pandas.DataFrame.columns)

We observed that len() involved in the functionality to get the size of the pandas objects and info() object not only returns the column names , but display other related information.

 

References

 

Views: 7

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment