We can use pandas.DataFrame.shape to print the shape of a dataset or the number of rows and columns of a dataset. In this article, we will read the dataset from a CSV file and then, use pandas.DataFrame.shape to print the shape of the dataset.
We can use the following Python code to print the number of rows and columns of a dataset.
import pandas
data = pandas.read_csv("iris.csv")
print(data.shape)
Here, we are first importing the pandas module. After that, we use the pandas.read_csv() function to read from a CSV file named “iris.csv”. The pandas.read_csv() file returns a DataFrame that contains data records from the CSV file.
The file “iris.csv” contains 150 records. Each record has five fields. They are sepal length, sepal width, petal length, petal width, and species of flowers. So, after reading from the CSV file, the DataFrame named data will contain the data records from this CSV file.
Now, we are using data.shape to print the shape of the dataset or the number of rows and columns of the dataset.
The output of the above program will be like the following:
(150, 5)
The output indicates that the dataset has 150 rows, and each row has 5 columns.








































0 Comments