代码
import pandas as pd
data = {
'sno':['14770110', '14770111', '14770112', '14770113', '14730114'],
'sname':['李白', '赵云', '干将', '鲁班', '花木兰'],
'sex':['man', 'woman', 'man', 'woman', 'man'],
}
df = pd.DataFrame(data)
print(df)
print('='*30)
print(df.sex == 'woman')
print('='*30)
print(type(df.sex == 'woman'))
print('='*30)
print(df[df.sex == 'woman'])
print('='*30)
print(df['sex'].value_counts())
print('='*30)
print(type(df['sex'].value_counts()))
print('='*30)
print(df.groupby(by = ['sex']))
print('='*30)
print(df.groupby(by = ['sex']).count())
print('='*30)
print(df.groupby(by = ['sex']).count().reset_index())
运行结果
sno sname sex
0 14770110 李白 man
1 14770111 赵云 woman
2 14770112 干将 man
3 14770113 鲁班 woman
4 14730114 花木兰 man
==============================
0 False
1 True
2 False
3 True
4 False
Name: sex, dtype: bool
==============================
<class 'pandas.core.series.Series'>
==============================
sno sname sex
1 14770111 赵云 woman
3 14770113 鲁班 woman
==============================
man 3
woman 2
Name: sex, dtype: int64
==============================
<class 'pandas.core.series.Series'>
==============================
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000001E1117EA710>
==============================
sno sname
sex
man 3 3
woman 2 2
==============================
sex sno sname
0 man 3 3
1 woman 2 2
[Finished in 0.8s]