Pandas Print DataFrame Size
In this short article, we will illustrate how to get the size of a Pandas DataFrame using the size property.
Sample DataFrame
The code below provides a sample DataFrame for illustration purposes throughout this article.
# import pandas
import pandas as pd
df = pd.DataFrame({
'salary': [120000, 100000, 90000, 110000, 120000, 100000, 56000],
'department': ['game developer', 'database developer', 'front-end developer', 'full-stack developer', 'database developer', 'security researcher', 'cloud-engineer'],
'rating': [4.3, 4.4, 4.3, 3.3, 4.3, 5.0, 4.4]},
index=['Alice', 'Michael', 'Joshua', 'Patricia', 'Peter', 'Jeff', 'Ruth'])
print(df)
import pandas as pd
df = pd.DataFrame({
'salary': [120000, 100000, 90000, 110000, 120000, 100000, 56000],
'department': ['game developer', 'database developer', 'front-end developer', 'full-stack developer', 'database developer', 'security researcher', 'cloud-engineer'],
'rating': [4.3, 4.4, 4.3, 3.3, 4.3, 5.0, 4.4]},
index=['Alice', 'Michael', 'Joshua', 'Patricia', 'Peter', 'Jeff', 'Ruth'])
print(df)
The code above should return a DataFrame as shown:
Pandas DataFrame Size Attribute
The size attribute returns the size of a DataFrame or Series. This corresponds to the total number of elements in the DataFrame.
The total number of elements in a DataFrame = rows x columns.
The code below shows how to get the size of the DataFrame provided above.
print(df.size)
… 21
… 21
The above returns the total number of elements in the DataFrame.
To get the number of rows and columns in a tuple format, you can use the shape attribute as shown:
print(df.shape)
This should return:
(7, 3)
Conclusion
This article shows how to get the size of a Pandas DataFrame using the size and shape attributes. Check the docs for more.
Source: linuxhint.com