假設我的資料如下所示:
| 新聞標題 | 公司 |
|---|---|
| 細繩 | |
| 細繩 | |
| 細繩 | 亞馬遜 |
| 細繩 | 蘋果 |
| 細繩 | 亞馬遜 |
| 細繩 |
如何對公司進行分組并獲得金額最大的公司的名稱和編號?
我希望能夠列印如下內容:
Facebook 在新聞中被提及最多 - 24 次。
我試過這個,但它沒有按我想要的方式作業:
df.groupby("company").sum()
uj5u.com熱心網友回復:
使用value_counts:
>>> df.company.value_counts().head(1)
Facebook 3
Name: company, dtype: int64
更新:
你能告訴我如何用一句話把它列印出來嗎?
company, count = list(df.company.value_counts().head(1).items())[0]
print(f'{company} was mentioned in the news the most - {count} times.')
# Output:
Facebook was mentioned in the news the most - 3 times.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/334591.html
