Friday, December 27, 2024
Google search engine
HomeLanguagesCount the number of rows and columns of a Pandas dataframe

Count the number of rows and columns of a Pandas dataframe

Pandas allow us to get the shape of the Dataframe by counting the number of rows and columns in the Dataframe. You can try various approaches to learn to count the number of rows and columns in a Pandas.

Example:

Input: 
{'name':          ['Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura'],
  'score':         [98, 80, 60, 85, 49, 92],
  'age':           [20, 25, 22, 24, 21, 20],
  'qualify_label': ['yes', 'yes', 'no','yes', 'no', 'yes']}

Output: 
Number of Rows: 6
Number of Columns: 4

Methods to get Pandas DataFrame Row Count and Column Count

In Python Pandas Dataframe we have various methods provided by the library. Here We will see different methods:

  • Using len(df.axes[]) function
  • Using info() function
  • Using len() function
  • Using shape
  • Using the size
  • Using count() and index

Count the number of rows and columns of DataFrame using len(df.axes[]) function

Let’s take an example of a Dataframe that consists of data on students’ exam results. To get the number of rows, and columns we can use len(df.axes[]) function in Python.

Python3




# importing pandas
import pandas as pd
result_data = {'name': ['Katherine', 'James', 'Emily',
                        'Michael', 'Matthew', 'Laura'],
               'score': [98, 80, 60, 85, 49, 92],
               'age': [20, 25, 22, 24, 21, 20],
               'qualify_label': ['yes', 'yes', 'no',
                                 'yes', 'no', 'yes']}
 
# creating dataframe
df = pd.DataFrame(result_data, index=None)
 
# computing number of rows
rows = len(df.axes[0])
 
# computing number of columns
cols = len(df.axes[1])
 
print(df)
print("Number of Rows: ", rows)
print("Number of Columns: ", cols)


Output: 

              name    score    age     qualify_label
0     Katherine    98        20           yes
1      James          80        25           yes
2     Emily            60        22            no
3    Michael        85        24           yes
4    Matthew      49        21            no
5    Laura            92       20           yes
Number of Rows:  6
Number of Columns:  4

Count the number of rows and columns of Dataframe using Info() function

Pandas dataframe.info() function is used to get a concise summary of the Dataframe. Here we can see that we get a summary detail of the Dataframe that contains the number of rows and columns.

Python3




# importing pandas
import pandas as pd
 
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
                            'Michael', 'Matthew', 'Laura'],
                   'score': [98, 80, 60, 85, 49, 92],
                   'age': [20, 25, 22, 24, 21, 20],
                   'qualify_label': ['yes', 'yes', 'no',
                                     'yes', 'no', 'yes']})
 
print(df.info())


Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
 #   Column         Non-Null Count  Dtype 
---  ------         --------------  ----- 
 0   name           6 non-null      object
 1   score          6 non-null      int64 
 2   age            6 non-null      int64 
 3   qualify_label  6 non-null      object
dtypes: int64(2), object(2)
memory usage: 320.0+ bytes
None

Count the number of rows and columns of Dataframe using Len() function

The len() function returns the length rows of the Dataframe, we can filter a number of columns using the df.columns to get the count of columns.

Python3




# importing pandas
import pandas as pd
 
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
                            'Michael', 'Matthew', 'Laura'],
                   'score': [98, 80, 60, 85, 49, 92],
                   'age': [20, 25, 22, 24, 21, 20],
                   'qualify_label': ['yes', 'yes', 'no',
                                     'yes', 'no', 'yes']})
 
print(len(df))
print(len(df.columns))


Output:

6
4

Count the number of rows and columns of Dataframe using Shape

Here, we will try a different approach for calculating rows and columns of a Dataframe of the imported CSV file, and counting the rows and columns using df.shape.

Python3




# importing pandas
import pandas as pd
 
# importing csv file
df = pd.read_csv(
 
print(df.head())
 
# obtaining the shape
print("shape of dataframe", df.shape)
 
# obtaining the number of rows
print("number of rows : ", df.shape[0])
 
# obtaining the number of columns
print("number of columns : ", df.shape[1])


Output :

   sepal_length  sepal_width  petal_length  petal_width species
0           5.1          3.5           1.4          0.2  setosa
1           4.9          3.0           1.4          0.2  setosa
2           4.7          3.2           1.3          0.2  setosa
3           4.6          3.1           1.5          0.2  setosa
4           5.0          3.6           1.4          0.2  setosa
shape of dataframe (150, 5)
number of rows :  150
number of columns :  5

Count the number of rows and columns of Dataframe using the Size

The size returns multiple rows and columns. i.e Here, the number of rows is 6, and the number of columns is 4 so the multiple rows and columns will be 6*4=24.

Python3




# importing pandas
import pandas as pd
 
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
                            'Michael', 'Matthew', 'Laura'],
                   'score': [98, 80, 60, 85, 49, 92],
                   'age': [20, 25, 22, 24, 21, 20],
                   'qualify_label': ['yes', 'yes', 'no',
                                     'yes', 'no', 'yes']})
 
print(df.size)


Output:

24

Count the number of rows of a Pandas Dataframe using Count() and Index

Using count() and index we can get the number of rows present in the Dataframe.

Python3




# importing pandas
import pandas as pd
 
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
                        'Michael', 'Matthew', 'Laura'],
               'score': [98, 80, 60, 85, 49, 92],
               'age': [20, 25, 22, 24, 21, 20],
               'qualify_label': ['yes', 'yes', 'no',
                                 'yes', 'no', 'yes']})
 
print(df[df.columns[0]].count())
print(len(df.index))


Output:

6
6

RELATED ARTICLES

Most Popular

Recent Comments