python中的value_counts()的用法

value_counts()是一种查看表格某列中有多少个不同值的快捷方法,并计算每个不同值有在该列中有多少重复值

1、value_counts()在Series的用法

import pandas as pd
import numpy as np
from pandas import DataFrame
from pandas import Series
s1=Series(["timo","mike","anni","timo"])
s1.value_counts()
timo    2
mike    1
anni    1
dtype: int64

2、value_counts()在DataFrame的用法

import pandas as pd
import numpy as np
from pandas import DataFrame
from pandas import Series
df1= DataFrame(
                {"handsome":["timo","anni","timo"],
                "smart":["mike","anni","mike"]}
                )
print(df1)
df1.apply(pd.value_counts)##数据框要借助apply来应用value_counts()
  handsome smart
0     timo  mike
1     anni  anni
2     timo  mike

 

python中的value_counts()的用法_第1张图片

 

 

你可能感兴趣的:(python)