python - counting all string values in given column of a table and grouping it based on third column
问题描述
I have three columns. the table looks like this:
ID. names tag
1. john. 1
2. sam 0
3. sam,robin. 1
4. robin. 1
Id: type integer Names: type string Tag: type integer (just 0,1)
What I want is to find how many times each name is repeated grouped by 0 and 1. this is to be done in python.
Answer must look like
0 1
John 23 12
Robin 32 10
sam 9 30
解决方案
Using extractall
and crosstab
:
s = df.names.str.extractall(r'(\w+)').reset_index(1, drop=True).join(df.tag)
pd.crosstab(s[0], s['tag'])
tag 0 1
0
john 0 1
robin 0 2
sam 1 1
推荐阅读
- html - CSS Grid - 如何在我的网格中调整视频的大小?
- python-3.x - 替换字符串中的第一个单词
- javascript - ReferenceError:在 HTMLInputElement.onclick 中未定义 submitForm
- javafx - 如何通过组 JavaFX 定位节点
- r - 无法将大型 ish .RDS 文件部署到 RStudio 中的 shinyapp.io
- xcode - 如何让用户快速登录
- dialogflow-es - Dialogflow - 从动态 URL 播放媒体响应 mp3 文件
- python - sympy : 检查并解析字符串中的表达式
- math - 两个函数的 Mathematica DSolve
- reactjs - 反应本机在新闻目标上获得“id”