0

I have generated a dataframe with following code. It gave multi grouped dataframe

dfwc = dffin.groupby('Borker')[['Netweight','TotPrice']].agg(['sum' ,'count'])

Borker,Sale_year,Saleno,Sale_dte,Fact_no,Cat_code,Grade,Gross_weight,Net_Weight,Lot_No,inv_year,Inv_no,Price,Netweight,BuyerRef,Buyer_code,SLReg,Btype
AS,2020,34,2/9/2020,MF1003 ,EXEST,BOPF      ,1163.5,1160,1,2020,0381R,540,1160,WC05 ,Watawala Tea Ceylon Ltd.                          ,1,M
AS,2020,34,2/9/2020,MF0663 ,EXEST,BOPF      ,1123.5,1120,4,2020,0165R,550,1120,WC05 ,Watawala Tea Ceylon Ltd.                          ,1,M
FW,2020,34,2/9/2020,MF0069 ,EXEST,BOP       ,963.5,960,5,2020,0278R,570,960,CM01 ,Ceylon Tea Marketing Ltd.                         ,1,M
CT,2020,34,2/9/2020,MF0069 ,EXEST,BOPF      ,1103.5,1100,6,2020,0282R,580,1100,CM01 ,Ceylon Tea Marketing Ltd.                         ,1,M
FW,2020,34,2/9/2020,MF0348 ,EXEST,BOPF      ,1163.5,1160,7,2020,0259R,570,1160,CM01 ,Ceylon Tea Marketing Ltd.                         ,1,M
CT,2020,34,2/9/2020,MF0348 ,EXEST,BOPF      ,1163.5,1160,8,2020,0264R,560,1160,TT01 ,Tea Tang (Pvt) Ltd                                ,0,M
MC,2020,34,2/9/2020,MF0703 ,EXEST,BOPF      ,1123.5,1120,9,2020,0193R,540,1120,AB01 ,Akbar Brothers (Pvt) Ltd                          ,1,M
MC,2020,34,2/9/2020,MF0552 ,EXEST,BOPF      ,1123.5,1120,11,2020,266,520,1120,AB01 ,Akbar Brothers (Pvt) Ltd                          ,1,M
JK,2020,34,2/9/2020,MF0294 ,EXEST,BOP       ,1003.5,1000,12,2020,0097R,560,1000,UL01 ,"Unilever Lipton Ceylon Ltd, Tea Division          ",1,M
JK,2020,34,2/9/2020,MF0981 ,EXEST,BOP       ,1003.5,1000,14,2020,0189R,580,1000,HC03 ,Harrisons (Colombo) Ltd.                          ,1,M
LC,2020,34,2/9/2020,MF0981 ,EXEST,BOPF      ,1123.5,1120,15,2020,0183R,590,1120,TL01 ,TDK Trade links (Pvt) Ltd.                        ,1,M
LC,2020,34,2/9/2020,MF0981 ,EXEST,BOPF      ,1123.5,1120,16,2020,0186R,590,1120,TL01 ,TDK Trade links (Pvt) Ltd.                        ,1,M
MB,2020,34,2/9/2020,MF0396 ,EXEST,BOPSp     ,1003.5,1000,17,2020,0223R,570,1000,MR01 ,Mabroc Teas (Pvt) Ltd.                            ,1,M
MB,2020,34,2/9/2020,MF0396 ,EXEST,BOPF      ,1163.5,1160,18,2020,0230R,590,1160,TL01 ,TDK Trade links (Pvt) Ltd.                        ,1,M
BC,2020,34,2/9/2020,MF0396 ,EXEST,BOPF      ,1163.5,1160,19,2020,0232R,570,1160,AB01 ,Akbar Brothers (Pvt) Ltd                          ,1,M
FW,2020,34,2/9/2020,MF0772 ,EXEST,BOP       ,1003.5,1000,20,2020,0213R,600,1000,VL01 ,Venture Tea Pvt. Ltd.                             ,1,M
EB,2020,34,2/9/2020,MF0772 ,EXEST,BOPF      ,1103.5,1100,21,2020,0214R,610,1100,CM01 ,Ceylon Tea Marketing Ltd.                         ,1,M
AS,2020,34,2/9/2020,MF0489 ,EXEST,BOP       ,1003.5,1000,22,2020,0194R,590,1000,UL01 ,"Unilever Lipton Ceylon Ltd, Tea Division          ",1,M
MB,2020,34,2/9/2020,MF0489 ,EXEST,BOPF      ,1123.5,1120,23,2020,0197R,560,1120,WL02 ,Walters Bay Bogawantalawa Estate (Pvt) Ltd.       ,1,M
CT,2020,34,2/9/2020,MF0711 ,EXEST,BOPSp     ,963.5,960,24,2020,0230R,600,960,DC02 ,Dilmah Ceylon Tea Company PLC                     ,1,M
CT,2020,34,2/9/2020,MF0711 ,EXEST,BOPF      ,1163.5,1160,25,2020,0233R,590,1160,WC05 ,Watawala Tea Ceylon Ltd.                          ,1,M

I have tried to plot with following code snip to generate bar graph.

dfwc.reset_index().plot(x='Borker', y = 'Netweight' , kind="bar")

I want to generate a subplot of tottal weight and total price ( Total_Price = ['Price'] * ['Netweight']).

Hope one of expert could help me in this regard.

hiran
  • 69
  • 5
  • Please [don't paste data as images](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Please provide the error message. – Bill Huang Oct 12 '20 at 09:33
  • I have managed to correct the error. But, still struggling to generate sub plot with Total_Price and Total Weight. – hiran Oct 14 '20 at 09:26
  • I cannot understand what you mean by "subplot of total weight and total price". Is it total weight against total price, or vice versa? Both sound a little strange to me. Please define **what is the x axis**, **what is the y axis** and **what are the data on the plot** explicitly. – Bill Huang Oct 14 '20 at 09:52
  • And what is "total weight"? And the name of total price is still inconsistent. – Bill Huang Oct 14 '20 at 09:54
  • Will this be the final form of the data? `dfwc = dffin.groupby('Borker')[['Netweight','Price']].agg(['sum'])` – r-beginners Oct 14 '20 at 09:55
  • If the content of the data is correct, you can output the following code. `dfwc.reset_index().plot(x='Borker', y = ['Netweight','Price'] , kind="bar")` – r-beginners Oct 14 '20 at 09:57

1 Answers1

0

Just use df.plot(kind="barh") instead of kind=bar.

dfwc.reset_index().plot(x='Borker', y='Total_Price', kind="barh", title="Price vs. Broker")

Imgur

Borker vs Total_Weight works the same way. See also the official example.

Note:

The columns must be renamed to remove hierarchical names. E.g.

dfwc.columns = ["Total_Weight", "count", "Total_Price", "count2"]

And the desired Total_Price was generated this way. (I cannot make sure because the OP was inconsistent on naming.).

# this    
dffin["Total_Price"] = dffin["Price"] * dffin['Netweight']
dfwc = dffin.groupby('Borker')[['Netweight','Total_Price']].agg(['sum' ,'count'])

# could also be this but not sure
dfwc = dffin.groupby('Borker')[['Netweight','Price']].agg(['sum' ,'count'])
Bill Huang
  • 4,491
  • 2
  • 13
  • 31
  • I just ask that, when I run this code on the data set (dfwc = dffin.groupby('Borker')[['Netweight','TotPrice']].agg(['sum' ,'count'])), I can get dataframe includin count,sum of weight and total price. With the generated dataframe , how can I plot 'broker' vs 'Total_Weight' and 'Broker' vs 'Total_Price'. – hiran Oct 15 '20 at 13:55
  • 'TotPrice' column does not exist so I cannot reuse your code to reproduce `dfwc`. – Bill Huang Oct 15 '20 at 14:21
  • Anyway I happened to have time to make another revision. Please take a look and see whether I have got your main point of the problem. – Bill Huang Oct 15 '20 at 14:35