I have this:
我有这个:
df = DataFrame(dict(person= ['andy', 'rubin', 'ciara', 'jack'],
item = ['a', 'b', 'a', 'c'],
group= ['c1', 'c2', 'c3', 'c1'],
age= [23, 24, 19, 49]))
df:
age group item person
0 23 c1 a andy
1 24 c2 b rubin
2 19 c3 a ciara
3 49 c1 c jack
what I want to do, is to get the length of unique items in each column. Now I know I can do something like:
我想要做的是获得每列中唯一项目的长度。现在我知道我可以这样做:
len(df.person.unique())
for every column.
对于每一列。
Is there a way to do this in one go for all columns?
有没有办法一次性完成所有列?
I tried to do:
我试着做:
for column in df.columns:
print(len(df.column.unique()))
but I know this is not right.
但我知道这不对。
How can I accomplish this?
我怎么能做到这一点?
3 个解决方案
#1
3
you want pd.Series.nunique
你想要pd.Series.nunique
df.apply(pd.Series.nunique)
age 4
group 3
item 3
person 4
dtype: int64
#2
2
You can the number of unique items in each column as:
您可以将每列中的唯一项目数量设置为:
for column in df.columns:
print(len(df[column].unique()))
#3
2
You can use:
您可以使用:
for column in df:
print(len(df[column].unique()))
4
3
3
4
Or:
要么:
for column in df:
print(df[column].nunique())
4
3
3
4
#1
3
you want pd.Series.nunique
你想要pd.Series.nunique
df.apply(pd.Series.nunique)
age 4
group 3
item 3
person 4
dtype: int64
#2
2
You can the number of unique items in each column as:
您可以将每列中的唯一项目数量设置为:
for column in df.columns:
print(len(df[column].unique()))
#3
2
You can use:
您可以使用:
for column in df:
print(len(df[column].unique()))
4
3
3
4
Or:
要么:
for column in df:
print(df[column].nunique())
4
3
3
4