w3resource

Python Scikit learn: Print the shape of the data, type of the data and first 3 rows

Python Machine learning Iris Basic: Exercise-1 with Solution

Write a Python program to load the iris data from a given csv file into a dataframe and print the shape of the data, type of the data and first 3 rows.

Sample Solution:

Python Code:

import pandas as pd
data = pd.read_csv("iris.csv")
print("Shape of the data:")
print(data.shape)
print("\nData Type:")
print(type(data))
print("\nFirst 3 rows:")
print(data.head(3))

Sample Output:

Shape of the data:
(150, 6)

Data Type:
<class 'pandas.core.frame.DataFrame'>

First 3 rows:
   Id  SepalLengthCm  SepalWidthCm  PetalLengthCm  PetalWidthCm      Species
0   1            5.1           3.5            1.4           0.2  Iris-setosa
1   2            4.9           3.0            1.4           0.2  Iris-setosa
2   3            4.7           3.2            1.3           0.2  Iris-setosa

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Python Machine learning Iris Basic Exercises Home.
Next: Write a Python program using Scikit-learn to print the keys, number of rows-columns, feature names and the description of the Iris data.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/machine-learning/scikit-learn/iris/python-machine-learning-scikit-learn-iris-basic-exercise-1.php