簡體   English   中英

在 pandas 中使用 groupby 查找和最常見的字符串

[英]Finding and most frequent string using groupby in pandas

我正在嘗試查找在一系列年份中任何給定年份提交最多申請的人的姓名。

每個應用程序都是數據框中自己的行。 它帶有提交的year和申請人的姓名。

我嘗試使用groupby按年份和名稱組織數據,然后使用value_counts()count()max()等各種方法...

這是我得到的最接近的:

df3.groupby(['app_year_start'])['name'].value_counts().sort_values(ascending=False)

它產生以下輸出:

app_year_start        name               total_apps
2015                  John Smith         622
2013                  John Smith         614
2014                  Jane Doe           611
2016                  Jon Snow           549

我想要的輸出:

app_year_start        name                  total_apps
2015                  top_applicant         max_num
2014                  top_applicant         max_num
2013                  top_applicant         max_num
2012                  top_applicant         max_num

一些虛擬數據行:

app_year_start        name
2012                  John Smith
2012                  John Smith
2012                  John Smith
2012                  Jane Doe
2013                  Jane Doe
2012                  John Snow
2015                  John Snow
2014                  John Smith
2015                  John Snow
2012                  John Snow
2012                  John Smith
2012                  John Smith
2012                  John Smith
2012                  John Smith
2012                  Jane Doe
2013                  Jane Doe
2012                  John Snow
2015                  John Snow
2014                  John Smith
2015                  John Snow
2012                  John Snow
2012                  John Smith

我已經查閱了以下 SO 帖子:

使用 pandas GroupBy 獲取每個組的統計信息(例如計數、平均值等)?

Pandas groupby nlargest sum

獲取 pandas groupby 對象的最大 count() 函數

我做過的其他一些嘗試:

df3.groupby(['app_year_start'])['name'].value_counts().sort_values(ascending=False)

df3.groupby(['app_year_start','name']).count()

任何幫助,將不勝感激。 我也對完全不同的解決方案持開放態度。

交叉制表並找到最大值。

(
    # cross tabulate to get each applicant's number of applications
    pd.crosstab(df['app_year_start'], df['name'])
    # the applicant with most applications and their counts
    .agg(['idxmax', 'max'], 1)
    # change column names
    .set_axis(['name','total_apps'], axis=1)
    # flatten df
    .reset_index()
)

結果

您可以按組使用mode

df.groupby('app_year_start')['name'].agg(lambda x: x.mode().iloc[0])

或者,如果您希望在出現平局的情況下將所有值作為單個字符串連接:

df.groupby('app_year_start')['name'].agg(lambda x: ', '.join(x.mode()))

輸出:

app_year_start
2012    John Smith
2013      Jane Doe
2014    John Smith
2015     John Snow
Name: name, dtype: object

初始代碼的變體:

(df
 .groupby(['app_year_start', 'name'])['name']
 .agg(total_apps='count')
 .sort_values(by='total_apps', ascending=False)
 .reset_index()
 .groupby('app_year_start', as_index=False)
 .first()
 )

輸出:

   app_year_start        name  total_apps
0            2012  John Smith           8
1            2013    Jane Doe           2
2            2014  John Smith           2
3            2015   John Snow           4

使用value_countsgroupby

dfc = (df.value_counts().reset_index().groupby('app_year_start').max()
          .sort_index(ascending=False).reset_index() 
          .rename(columns={0:'total_apps'})
      )

print(dfc)

結果

   app_year_start        name  total_apps
0            2015   John Snow           4
1            2014  John Smith           2
2            2013    Jane Doe           2
3            2012   John Snow           8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM