Reputation: 95
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:
df.groupby(['weight_conversion'])['Sales_Unit'].sum()
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.**
Upvotes: 0
Views: 535
Reputation: 1625
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:
Upvotes: 1