如何使用熊猫中的groupby总计客户的总收入

发布于 2025-01-19 18:48:33 字数 387 浏览 1 评论 0原文

我正在使用一个数据库,但是我只有一件事就

我要显示的一部分在下图中:

“在此处输入图像描述”

此数据库包括每个客户及其各自的计划,由“ Plano”列

“ Valor”列和“ LT_Month”列分别表示订阅值和客户寿命(以几个月为单位)

表示最后一列代表客户给我们的总收入。

记住,在此打印屏幕中,这两行代表同一客户。因此,我想变成一个总收入为75的单个客户。

I'm working with a database but I'm having a problem with only one thing

The part I want to show is in the image below:

enter image description here

This database includes each customer and their respective plan, indicated by the 'plano' column

The 'valor' column and the 'lt_month' column represent, respectively, the subscription value and the customer lifetime (in months)

The last column represents the total revenue the customer has given us.

Remembering that, in this print screen, both lines represent the same customer. So I would like to turn into a single customer with total revenue of 75.

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

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

发布评论

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

评论(5

与风相奔跑 2025-01-26 18:48:33

您可以使用groupby函数聚合结果。

df.groupby('id').agg({'revenue':'sum'})

You can aggregate the results by using groupby function.

df.groupby('id').agg({'revenue':'sum'})
忘年祭陌 2025-01-26 18:48:33

您可以使用功能groupby,然后agg来汇总结果,以计算收入的总和。

df = pd.DataFrame(data={'id': [1, 2, 1], 'revenue': [10, 20, 30]})
agg_df = df.groupby(by=['id']).agg({'revenue': 'sum'})

You can aggregate the results using the function groupby and then agg to calculate the sum of the revenue.

df = pd.DataFrame(data={'id': [1, 2, 1], 'revenue': [10, 20, 30]})
agg_df = df.groupby(by=['id']).agg({'revenue': 'sum'})
别忘他 2025-01-26 18:48:33
import pandas as pd

group_df = pd.DataFrame(data=[
    ['MI','P1','Male',54,15],
    ['MI','P2','Female',21,19],
    ['DD','P3','Male',69,26],
    ['RR','P4','Female',96,28],
    ['GT','P5','Male',33,24],
    ['MI','P6','Female',51,33],
    ['KNR','P7','Male',24,40],
    ['GT','P8','Male',36,42],
    ['RR','P9','Female',78,19],
    ['KNR','P10','Male',33,17],
    ['MI','P11','Female',87,20],
    ['GT','P12','Male',81,21],
    ['KNR','P13','Female',36,29]],
columns=['Team','Player','Sex','Score','Age'])

group_arg = group_df.groupby('Team')

# Returns a dataframe for Team 'MI'
group_arg.get_group('MI')
import pandas as pd

group_df = pd.DataFrame(data=[
    ['MI','P1','Male',54,15],
    ['MI','P2','Female',21,19],
    ['DD','P3','Male',69,26],
    ['RR','P4','Female',96,28],
    ['GT','P5','Male',33,24],
    ['MI','P6','Female',51,33],
    ['KNR','P7','Male',24,40],
    ['GT','P8','Male',36,42],
    ['RR','P9','Female',78,19],
    ['KNR','P10','Male',33,17],
    ['MI','P11','Female',87,20],
    ['GT','P12','Male',81,21],
    ['KNR','P13','Female',36,29]],
columns=['Team','Player','Sex','Score','Age'])

group_arg = group_df.groupby('Team')

# Returns a dataframe for Team 'MI'
group_arg.get_group('MI')
束缚m 2025-01-26 18:48:33

您可以按功能使用熊猫组,并通过客户和收入列

df = df.groupby("customer")["revenue"].sum()

You can use pandas group by function and pass customers and revenue columns

df = df.groupby("customer")["revenue"].sum()
别挽留 2025-01-26 18:48:33

如果我理解正确的话,你只需要使用 groupby 。
我假设“Cod”列标识了客户,因此:

Df.groupby("Cod")["total_revenue"].sum()

If I understand you correctly you just need to use groupby.
I'm assuming the columns "Cod" identifies the customer, so:

Df.groupby("Cod")["total_revenue"].sum()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文