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("\nData Type:")
print("\nFirst 3 rows:")

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?

Follow us on Facebook and Twitter for latest update.