在Python中,统计列的项数可以通过以下几种方法实现:
1. 使用`len()`函数统计整个列的项数:
import pandas as pd
创建一个示例数据表
data = {'col1': [1, 2, 3, 4, 5, 5, 5], 'col2': [1, 1, 2, 2, 3, 3, 3]}
df = pd.DataFrame(data)
统计某列不同值的个数
unique_count = df['col2'].nunique()
print(unique_count) 输出:3
2. 使用`value_counts()`函数统计某列中各个唯一值的个数:
统计某列中各个唯一值的个数
value_counts = df['col2'].value_counts()
print(value_counts) 输出:33
22
12
Name: col2, dtype: int64
3. 使用`count()`方法统计某列中某个特定值出现的次数:
统计某列中某个特定值出现的次数
count_value = df['col2'].count()
print(count_value) 输出:6
以上方法适用于Pandas库中的DataFrame对象。如果你使用的是列表,可以使用Python内置的`len()`函数或者列表的`count()`方法来统计项数。