- 8 methods to get size of Pandas Series/DataFrame Object
- Method 1 : Use pandas.Series.str.len() to get size of values in Series
- Method 2 : Use pandas.Series.size() to list total number of values in Series
- Method 3 : Get DataFrame object size using pandas.DataFrame.size()
- Example 1: Get entire Pandas DataFrame size
- Example 2: Get number of elements per column in DataFrame
- Method 4 : Get number of rows, columns, etc in detail with pandas.DataFrame.info()
- Method 5 : pandas.DataFrame.shape
- Method 6 : pandas.DataFrame/Series.ndim
- Example 1: Get the dimension of Pandas DataFrame
- Example 2: Get the dimension of Pandas Series
- Method 7 : Get number of values from Series or DataFrame using len()
- Example 1: Get number of elements in a Panda Series
- Example 2: Get number of rows in Pandas DataFrame
- Method 8 : Get number of columns in DataFrame using len(pandas.DataFrame.columns)
- Method 9 : Get number of rows and columns in DataFrame using len(pandas.DataFrame.axes)
- Example 1: Get the number of columns in Pandas DataFrame
- Example 2: Get number of rows in Pandas DataFrame
- Summary
- References
8 methods to get size of Pandas Series/DataFrame Object
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 .
where, data is the input Series
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())
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.
where, data is the input Series
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)
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.
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.
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() # display the dataframe print(data, "\n===================") # function - dataframe.size print("Number of objects in Panda DataFrame: \n", data.size)
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() # 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)
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.
where, data is the input dataframe
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() # display the dataframe print(data) # function -info() print(data.info())
Name Age Marks 0 sai 21 89 1 sravan 22 87 2 deepak 31 89 3 prasad 34 90 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.
where, data is the input dataframe
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() # display the dataframe print(data, "\n===================\n") # function -shape print("Number of rows, columns: ", data.shape)
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.
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() # display the dataframe print(data, "\n===================\n") # function -ndim print("Get the DataFrame dimension: ", data.ndim)
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)
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.
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))
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() # display the dataframe print(data, "\n===================\n") # function -len print("Number of rows in DataFrame: ", len(data))
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.
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() # display the dataframe print(data, "\n===================\n") # function -len print("Number of columns in DataFrame: ", len(data.columns))
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
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() # display the dataframe print(data, "\n===================\n") # function -len print("Number of columns in DataFrame: ", len(data.axes[1]))
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() # display the dataframe print(data, "\n===================\n") # function -len print("Number of rows in DataFrame: ", len(data.axes[0]))
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.
- pandas.Series.str.len()
- pandas.Series.size()
- pandas.DataFrame.size()
- pandas.DataFrame.info()
- pandas.DataFrame.shape
- pandas.DataFrame/Series.ndim
- len()
- 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
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!