这是我的文本文件的外观
000000005|19670905|M|20060201|20070131|6709055223085|01|PRINCIPLE|000021629633|ONYX
000000005|19740423|F|20060201|20070131|7404230424084|01|WIFE|000021629633|ONYX
000000005|19991028|F|20060201|20070131|9910280147084|01|DAUGHTER|000021629633|ONYX
我需要遇到单词PRINCIPLE
,然后提供WIFE
和DAUGHTER
的数量
这里WIFE
的计数为1,而DAUGHTER
的计数也为1
这些列和行都有标题
有多个条目,例如000004
,000008
等
counts = data['gender'].value_counts().to_dict()
我这样做是为了获得男性和女性的人数。我只是在尝试。我需要一些有关如何使用python解决此问题的帮助
我想要类似
PRINCIPLE WIFE DAUGHTER
and below the counts
答案 0 :(得分:1)
您可以尝试
import pandas as pd
# select the gender col
gender = data[['gender']]
# groupby to a new dataframe
counts = pd.DataFrame({'count' : gender.groupby(['gender']).size()}).reset_index()
如果要添加第一个列“条目”
gender = data[['gender','entries']]
# groupby to a new dataframe
counts = pd.DataFrame({'count' : gender.groupby(['entries','gender']).size()}).reset_index()
示例:
>>> print(d)
entries gender
0 5 PRINCIPLE
1 5 WIFE
2 5 DAUGHTER
3 6 PRINCIPLE
4 6 PRINCIPLE
5 6 DAUGHTER
6 7 WIFE
7 7 DAUGHTER
8 7 WIFE
>>> count = pd.DataFrame({'count' : d.groupby(['entries','gender']).size()}).reset_index()
>>> print(count)
entries gender count
0 5 DAUGHTER 1
1 5 PRINCIPLE 1
2 5 WIFE 1
3 6 DAUGHTER 1
4 6 PRINCIPLE 2
5 7 DAUGHTER 1
6 7 WIFE 2