Skill - Getting the shape/dimension of DataFrame
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- Pandas DataFrame Basics
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
Pandas is a python library.
DataFrame is a data structure provided by the pandas library.
Please go through Pandas DataFrame Basics to learn the basics of pandas DataFrame.
In this post, we will learn how to get the shape of a pandas dataframe using shape attribute
The shape attribute returns the shape of DataFrame as (nRows, nColumns)
import pandas as pd
# create a dataframe
x = {
"Name": ["Harris", "Miss. Elizabeth"],
"Age": [22, 58],
"Sex": ["male", "female"]
}
df = pd.DataFrame(x)
print(df.shape)
# this will print (2,3)
print('Number of rows = {0}'.format(df.shape[0]))
print('Number of columns = {0}'.format(df.shape[1]))
'''
this will print
Number of rows = 2
Number of columns = 3
'''
Video
Video for this post can be found here
Online Interpreter
Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://www.tutorialspoint.com/execute_python_online.php
References
- Official tutorial - https://pandas.pydata.org/pandas-docs/stable/getting_started/index.html
Comments
Post a Comment