sepal_length sepal_width petal_length petal_width species 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa Unique species: ['setosa' 'versicolor' 'virginica']
So, there are three unique species ‘setosa’, ‘versicolor’, and ‘virginica’ in the species column of the DataFrame. Now, we want to find out how many records contain ‘setosa’, ‘versicolor’, and ‘virginica’ in the species column, respectively. We can use the value_counts() function in the pandas Python library to find out the same.
import pandas
df = pandas.read_csv("iris.csv")
print(df.head())
print("Unique species: \n", df["species"].unique())
print("Count of each unique values: \n", df["species"].value_counts())
The output will be:
sepal_length sepal_width petal_length petal_width species 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa Unique species: ['setosa' 'versicolor' 'virginica'] Count of each unique values: setosa 50 versicolor 50 virginica 50 Name: species, dtype: int64
So, there are 50 records that have “setosa” in the species column of the DataFrame, and so on.








































0 Comments