python的枢轴表(熊猫)

发布于 2025-02-11 11:22:47 字数 1854 浏览 2 评论 0原文

我是Python(使用Pandas)的新手。请帮助。

我的数据框中有两个列 - 权重转换(float)和sales_units(int)。 我只想按(sum)通过wigath_conversion进行sales_units。

示例数据

weight_conversion   sales_units
0.1                   1
0.1                   2
50                    100
50                    200
96.1                  20
314.4                  2
500                   100
500                   200

            

我尝试了两种方法:

  1. by in pandas:complat by in pandas:

df.groupby(['stoge_conversion'])['sales_unit']。sum()

  1. df.groupby([[ '

    df.pivot_table(index ='stroge_conversion',values ='sales_unit', aggfunc ='sum')

必需的输出:我需要一个简单的枢轴表,其中我的行作为weight_conversion以及销售单元的总和。

我在python pandas中获得的输出如下(很奇怪): weight_conversion

0       3300000000000000000000000000034000000000000000...
0.1     0000100001000000000000001000000020050000000000...
0.2     0000000000000000000000000000001000000001100000...
0.3                           000000000000000000000300000
0.4     0000000000100000000000000000000000000000000001...
                            ...                        
90                                        000000000102009
92      0000200011000000000000010001000000000000000020...
92.1                            0000001000000000000000003
96      2000000000000000000000000000000000001100000000...
96.1    0000000000000000000000000000000000000000000000...

名称:sales_unit,长度:96,dtype:对象

样本输出

weight_conversion   sales_units
0.1                   3
50                    300
96.1                  20
314.4                  2
500                   300

请帮助。**

I'm very new to python (using pandas). Kindly help.

There are two columns in my dataframe - weight conversion (float) and sales_units (int). I simply want to group by (sum) the sales_units by weight_conversion.

Sample Data

weight_conversion   sales_units
0.1                   1
0.1                   2
50                    100
50                    200
96.1                  20
314.4                  2
500                   100
500                   200

            

I have tried two ways:

  1. GROUP BY IN PANDAS :

df.groupby(['weight_conversion'])['Sales_Unit'].sum()

  1. PIVOT TABLE IN PANDAS:

    df.pivot_table(index = 'weight_conversion', values='Sales_Unit',
    aggfunc ='sum')

Required Output: I need a simple pivot table where I have rows as weight_conversion along with sum of sales units.

The output I'm getting in Python Pandas is as follows (so weird):
weight_conversion

0       3300000000000000000000000000034000000000000000...
0.1     0000100001000000000000001000000020050000000000...
0.2     0000000000000000000000000000001000000001100000...
0.3                           000000000000000000000300000
0.4     0000000000100000000000000000000000000000000001...
                            ...                        
90                                        000000000102009
92      0000200011000000000000010001000000000000000020...
92.1                            0000001000000000000000003
96      2000000000000000000000000000000000001100000000...
96.1    0000000000000000000000000000000000000000000000...

Name: Sales_Unit, Length: 96, dtype: object

sample output

weight_conversion   sales_units
0.1                   3
50                    300
96.1                  20
314.4                  2
500                   300

Please help.**

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

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

发布评论

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

评论(1

另类 2025-02-18 11:22:47

我认为您不需要枢轴表才能获得所提供的样本输出。以下是您提到的所需输出的工作

df = pd.DataFrame({"weight_conversion":[0.1,0.1,50,50,96.1,314.4,500,500],
              "sales_units":[1,2,100,200,20,2,100,200]})
              
df.groupby('weight_conversion').agg({'sales_units':'sum'}).reset_index()

Output:

enter image description here

I dont think you need PIVOT table for the sample output you have given. Below worked as for the required output you mentioned

df = pd.DataFrame({"weight_conversion":[0.1,0.1,50,50,96.1,314.4,500,500],
              "sales_units":[1,2,100,200,20,2,100,200]})
              
df.groupby('weight_conversion').agg({'sales_units':'sum'}).reset_index()

Output:

enter image description here

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