简体   繁体   English

在每个数据框列中创建最频繁值的行

[英]Create row of most frequent value in each dataframe column

CONTEXT语境

I want to create a top row with the most frequent values of each column.我想用每列的最常见值创建一个顶行。

CURRENT CODE当前代码

df = df.loc[df['Gender'] == 'M']
df = df('Gender').count()

DATA SAMPLE数据样本

Gender      Eyes      Hair    Height
  M         Brown     Brown    >6ft
  M         Blue      Blonde   <6ft
  M         Brown     Blonde   <6ft

EXPECTED OUTCOME预期结果

Gender      Eyes      Hair    Height
  M         Brown     Blonde    <6ft   

It's exactly what the mode function in Pandas does.这正是 Pandas 中的mode函数所做的。 Assuming your data sample is df , you can obtain your expected output using the following line of code假设您的数据样本是df ,您可以使用以下代码行获得预期的输出

df.mode()

Check with mode检查mode

df.mode()
  Gender   Eyes    Hair Height
0      M  Brown  Blonde   <6ft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM