1

I have data calls SalesData that's contains "Profit", "Sales" and "Sub-Category" and when I use this code

SubCategoryProfit = SalesData[["Sub-Category", "Profit"]].groupby(by = "Sub-Category").sum().sort_values(by = "Profit")
  #Print the results
  SalesData.style.applymap(color_negative_red, subset=['Profit','Sales'])
  print(SubCategoryProfit)

I will get these results

                 Profit
Sub-Category            
Tables       -17725.4811
Bookcases     -3472.5560
Supplies      -1189.0995
Fasteners       949.5182
Machines       3384.7569
Labels         5546.2540
Art            6527.7870
Envelopes      6964.1767

however when I am looking for the negative results only with this code

JustSubCatProf = SalesData[["Sub-Category", "Profit"]]
NegProfFilter = SalesData["Profit"] < 0.0 
JustNegSubCatProf = JustSubCatProf[NegProfFilter].groupby(by = "Sub-Category").sum().sort_values(by = "Profit")
print(JustNegSubCatProf)

I will get this!

                 Profit
Sub-Category            
Binders      -38510.4964
Tables       -32412.1483
Machines     -30118.6682
Bookcases    -12152.2060
Chairs        -9880.8413
Appliances    -8629.6412
Phones        -7530.6235
Furnishings   -6490.9134
Storage       -6426.3038
Supplies      -3015.6219

There should be only 3 negative results I'm not sure what I am doing wrong. Can someone help me please?

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • With your first code snippet, you can use `SubCategoryProfit2 = SubCategoryProfit[SubCategoryProfit['Profit'] < 0]` to get only "negative profit rows" (which should be 3 rows). – cs95 Oct 29 '20 at 05:53
  • Perfect That Worked! Thank you so much @cs95 – Nikan Jafari Oct 29 '20 at 06:06

2 Answers2

1

You can create a new dataframe with negative values ​​by filtering as follows.

SalesData[SalesData["valuecol1"] < 0]

I cannot fully understand your problem as I cannot see exactly what data it has. If you can share some of the data, I can give a clearer answer.

1

Once you have this result:

In [2878]: df
Out[2878]: 
                  Profit
Sub-Category            
Tables       -17725.4811
Bookcases     -3472.5560
Supplies      -1189.0995
Fasteners       949.5182
Machines       3384.7569
Labels         5546.2540
Art            6527.7870
Envelopes      6964.1767

You can do this to get only -ve rows:

In [2880]: df[df.Profit.lt(0)]
Out[2880]: 
                  Profit
Sub-Category            
Tables       -17725.4811
Bookcases     -3472.5560
Supplies      -1189.0995
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58