value_counts()
时间: 2024-05-15 19:14:59 浏览: 93
value_counts() is a method in Pandas library in Python that can be used to count the number of occurrences of each unique value in a column or a Series. The method returns a Series with the count of unique values. It can be used to find out the frequency distribution of values in a dataset or to identify the most common values in a particular column.
Here is an example code snippet that demonstrates the usage of value_counts() method:
```
import pandas as pd
# Creating a sample dataframe
data = {'fruit': ['apple', 'banana', 'apple', 'orange', 'kiwi', 'banana', 'orange', 'orange']}
df = pd.DataFrame(data)
# Counting the number of occurrences of each unique value in column 'fruit'
counts = df['fruit'].value_counts()
print(counts)
```
Output:
```
orange 3
banana 2
apple 2
kiwi 1
Name: fruit, dtype: int64
```
In this example, we have created a sample dataframe with a column 'fruit' that contains different types of fruits. We have used the value_counts() method to count the number of occurrences of each unique fruit in the column 'fruit'. The method returns a Series with the count of unique fruits.
阅读全文