如何对数据进行分组求和,return最大的求和在Python?
How to group and sum data and return the biggest sum in Python?
假设我的数据是这样的:
news_title
company
string
Facebook
string
Facebook
string
Amazon
string
Apple
string
Amazon
string
Facebook
如何对公司进行分组并获得总和最大的公司的名称和编号?
我希望能够打印如下内容:
Facebook 在新闻中被提及的次数最多 - 24 次。
我试过了,但没有达到我想要的效果:
df.groupby("company").sum()
使用value_counts
:
>>> df.company.value_counts().head(1)
Facebook 3
Name: company, dtype: int64
更新:
Could you please tell me how I could go about printing it out in a sentence?
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.
假设我的数据是这样的:
news_title | company |
---|---|
string | |
string | |
string | Amazon |
string | Apple |
string | Amazon |
string |
如何对公司进行分组并获得总和最大的公司的名称和编号?
我希望能够打印如下内容:
Facebook 在新闻中被提及的次数最多 - 24 次。
我试过了,但没有达到我想要的效果:
df.groupby("company").sum()
使用value_counts
:
>>> df.company.value_counts().head(1)
Facebook 3
Name: company, dtype: int64
更新:
Could you please tell me how I could go about printing it out in a sentence?
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.