pandas表聚合后某列进行去重计数
时间: 2023-09-03 10:10:31 浏览: 117
Pandas 同元素多列去重的实例
Sure, I can help with that! To count the number of unique values in a column after aggregating a Pandas DataFrame, you can use the nunique() method. Here's an example:
```
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({
'group': ['A', 'B', 'A', 'B', 'A', 'B'],
'value': [1, 2, 3, 1, 2, 1]
})
# Aggregate the DataFrame by group and count the number of unique values of 'value'
counts = df.groupby('group')['value'].nunique()
print(counts)
```
This will output:
```
group
A 3
B 2
Name: value, dtype: int64
```
I hope that helps!
阅读全文