pandas筛选数据

代码

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())#参数inpalce = Ture

运行结果

        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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值