grouped.count()
时间: 2023-10-10 18:08:01 浏览: 59
时间序列整理知识.docx
`grouped.count()` is a method used in pandas library to count the number of non-null values in each group of a DataFrame. It returns a new DataFrame containing the count of non-null values for each group. Here's an example:
```python
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({
'group': ['A', 'A', 'B', 'B', 'B'],
'value': [1, 2, 3, 4, 5]
})
# group by 'group' column and get the count of non-null values in each group
grouped = df.groupby('group')
counted = grouped.count()
print(counted)
```
Output:
```
value
group
A 2
B 3
```
In this example, we grouped the DataFrame by the 'group' column and counted the number of non-null values in each group using `grouped.count()`. The output shows that group 'A' has 2 non-null values and group 'B' has 3 non-null values.
阅读全文