0

This is my data frame:

 "Column 1"         "Column 2"
    XYZ55 Data Tech    128
    XYZ59 Data Tech    117
    XYZ54 Data Tech     53
    XYZ64 Data Tech      7
    XYZ57 Data Tech     12
    XYZ56 Data Tech     10
    XYZ53 Data Tech      9

I got this from running the below on my original data frame:

counts = df["AssignedGroup"].value_counts(sort=False)

My desired data frame:

XYZ53 Data Tech    9
XYZ56 Data Tech    10
XYZ57 Data Tech    12
XYZ54 Data Tech    53
XYZ64 Data Tech    7
XYZ55 Data Tech    128
XYZ59 Data Tech    117

Am I able to manually pick the order that is outputted as a result of running value_counts(), an order that is not based on an ascending/descending/alphabetic order?

Kyzoki
  • 3
  • 2
  • Not sure if I understand you question, but sort=False orders the data based on the order each value was found first in that column. What is the logic behind your desired order? – Scott Boston Oct 19 '22 at 02:45
  • @ScottBoston there is no specific logic to my order, this is my point. My question was can I reindex/reorder my data frame such that I manually specify the indexes based on a given string. For example index 0 = "XYZ53 Data Tech" or index 1 = "XYZ56 Data Tech" – Kyzoki Oct 19 '22 at 02:53
  • Yes, you can. You can create your order using pd.Categorical and create your own order. Or set, 'Column 1' as the dataframe index, then use reindex with a list of strings in the order you want. – Scott Boston Oct 19 '22 at 02:55
  • Does this answer your question? [Custom sorting in pandas dataframe](https://stackoverflow.com/questions/13838405/custom-sorting-in-pandas-dataframe) – Riley Oct 19 '22 at 09:08

1 Answers1

0

Inherit and define your custom order.

from pandas import DataFrame
class myDataFrame(DataFrame):
    def value_counts(self, custom_order):
        print(f"Hello !! {custom_order}")
m = myDataFrame()
m.value_counts(custom_order="new_order")

>>> Hello !! new_order
Tanveer
  • 51
  • 1
  • 1
  • 7