计算 Pandas 数据框中不同列中相同单词的数量

发布于 2025-01-11 04:22:16 字数 278 浏览 0 评论 0原文

我有一个数据框,其中包含户主和家庭成员的不同人口统计数据。我想计算整个数据框中女性和男性的总数。 value_counts() 仅从一列中提取计数,如何计算所有列。 df['Column_Name'].value_counts() 可以获取包含相同值的多个列吗?

这些列是: 申请人姓名、申请人性别、家庭成员 2 姓名、家庭成员 2 性别、家庭成员 3 姓名、家庭成员 3 性别。

“申请人性别”、“家庭成员2性别”、“家庭成员3性别”栏中的值为“女”或“男”

I have a data frame that contains different demographic data of the head of the household and the family members. I would like to count the total number of females and males in the whole data frame. The value_counts() only pull the count from one column, how do I count all the columns. Can df['Column_Name'].value_counts() take multiple columns containing the same values?

The columns are:
Applicant Name, Applicant Gender, Household Member 2 Name, Household Member 2 Gender, Household Member 3 Name, Household Member 3 Gender.

The values in the columns of Applicant Gender, Household Member 2 Gender, and Household Member 3 Gender are either Female or Male

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2025-01-18 04:22:16

如果我的想法正确,这应该可行。

males = df[(df['Applicant Gender'] == 'Male') & (df['Household Member 2 Gender'] == 'Male') & (df['Household Member 3 Gender'] == 'Male')].count().sum()

females = df[(df['Applicant Gender'] == 'Female') & (df['Household Member 2 Gender'] == 'Female') & (df['Household Member 3 Gender'] == 'Female')].count().sum()

If I get the idea right, this should work.

males = df[(df['Applicant Gender'] == 'Male') & (df['Household Member 2 Gender'] == 'Male') & (df['Household Member 3 Gender'] == 'Male')].count().sum()

females = df[(df['Applicant Gender'] == 'Female') & (df['Household Member 2 Gender'] == 'Female') & (df['Household Member 3 Gender'] == 'Female')].count().sum()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文