0

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.**

4
  • 1
    Can you please provide some sample data as well as expected results? Commented Jun 29, 2022 at 3:45
  • @ArchAngelPwn: Just edited and provided sample. Appreciate your comment. It would mean so much if you could help. :) Commented Jun 29, 2022 at 3:53
  • 1
    What's the problem with your tried two ways? Commented Jun 29, 2022 at 4:08
  • @Ynjxsjmh: I think the problem was with my import data code df = pd.read_csv('df.csv', dtype=object). Once I removed dtype = object, the code worked. But I don't understand the logic. Can you explain why it worked after I removed the dtype = object? Commented Jun 29, 2022 at 4:10

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Abhishek, actually I figured out my error. Since my file had all mixed type data I imported using df = pd.read_csv('df.csv', dtype = object). Once I removed dtype=object, my code was successful. My code is same as yours. Thank you for your time. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.