## Counting Occurrences and Sorting in Pandas

Sorting and counting occurrences in Pandas can be a daunting task, especially if you are new to the programming language. However, with a little know-how, this can be easily achieved.

In this article, we discuss three methods of counting occurrences and sorting in Pandas.

### Method 1: Sort Counts in Descending Order

One of the most common tasks in Pandas is counting occurrences of unique values.

The `value_counts()`

function is used to sort and count occurrences of unique values, like this:

```
import pandas as pd
data = pd.Series([10, 20, 10, 40, 30, 20, 10, 40, 50])
counts = data.value_counts()
```

### This will give you the output:

```
10 3
40 2
20 2
50 1
30 1
dtype: int64
```

To sort the above output in descending order, you can use the `sort_values()`

function, like this:

```
counts_sorted = counts.sort_values(ascending=False)
```

### This will give you the output:

```
10 3
40 2
20 2
50 1
30 1
dtype: int64
```

Notice that the output is now sorted in descending order. The key to sorting the values in descending order is passing `ascending=False`

to `sort_values()`

.

In this example, “10” has the highest frequency of 3 and is listed first, followed by “40” and “20” with a frequency of 2, and finally “50” and “30” with a frequency of 1. Now, we can easily identify the most frequent values.

### Method 2: Sort Counts in Ascending Order

If we wanted to sort the values in ascending order instead, we can do so by adding the `ascending=True`

parameter to `sort_values()`

like this:

```
counts_sorted = counts.sort_values(ascending=True)
```

### This will give you the output:

```
30 1
50 1
40 2
20 2
10 3
dtype: int64
```

The output is now sorted in ascending order. The least frequent values are listed first, followed by the increasingly more frequent values.

### Method 3: Sort Counts in Order they Appear in DataFrame

If you wanted to sort the values in the order they appear in the original DataFrame or Series, you can simply call `value_counts()`

without any sorting parameters, like this:

```
counts = data.value_counts()
```

### This will give you the output:

```
10 3
20 2
40 2
30 1
50 1
dtype: int64
```

Notice that the output is now in the same order as the original `data`

Series. This can be useful when you want to quickly check the frequency of values in the same order they appear in the DataFrame.

### Example 1: Sort Counts in Descending Order

Let’s say we have a large DataFrame that contains information about restaurant orders, with the following columns: “order_id”, “customer_id”, “order_time”, “item_name”, and “item_quantity”. We want to count the number of times each item has been ordered and sort the values in descending order to identify the most frequently purchased items.

### We can do this using the following code:

```
import pandas as pd
# Load restaurant orders into DataFrame
df = pd.read_csv("restaurant_orders.csv")
# Count occurrences of each item
item_counts = df["item_name"].value_counts()
# Sort in descending order
most_ordered_items = item_counts.sort_values(ascending=False)
# Display the top 10 most frequently ordered items
print(most_ordered_items.head(10))
```

### This code will give you the output:

```
Chicken Sandwich 129
Hamburger 120
French Fries 103
Cheeseburger 97
Onion Rings 66
Coke 46
Chocolate Shake 43
Vanilla Shake 38
Spicy Chicken Sandwich 36
Sprite 33
Name: item_name, dtype: int64
```

We can quickly see that the chicken sandwich is the most frequently ordered item, followed by the hamburger, French fries, and cheeseburger. In conclusion, counting occurrences and sorting in Pandas can be used to quickly inspect and analyze large datasets.

With a few lines of code, you can easily count unique values, sort the values in ascending or descending order, and group them by other variables in your DataFrame. These methods can also be applied to other types of data analysis tasks, making them a valuable tool for any data analyst or scientist.

### Example 2: Sort Counts in Ascending Order

Sorting counts in ascending order is not as commonly used as sorting them in descending order, but it can be useful in certain situations. For instance, if we are comparing the frequencies of values in a dataset, sorting them in ascending order can make it easier to spot if any of the values have similar frequencies.

To better demonstrate this, let’s consider the following example:

```
import pandas as pd
# Create a Series with random numbers
data = pd.Series([10, 20, 10, 40, 30, 20, 10, 40, 50])
# Count the number of occurrences of unique values
counts = data.value_counts()
# Sort the counts in ascending order
ascending_counts = counts.sort_values(ascending=True)
# Print the sorted counts
print(ascending_counts)
```

### The output will be:

```
30 1
50 1
40 2
20 2
10 3
dtype: int64
```

From this output, we can see that the values “30” and “50” have identical frequencies of 1, and therefore, it is easier to spot that they have similar frequencies since the output is sorted in ascending order.

### Example 3: Sort Counts in Order they Appear in DataFrame

Lastly, we can sort counts in the order they appear in the DataFrame.

Sorting the counts in the order they appear in the DataFrame will help us get a better understanding of how frequently the values appear in the original data set. Let’s consider an example of a DataFrame of a toy store that has information such as the toy names, the age group they are meant for, and the price of each toy.

We want to count the number of times each toy name appears in the DataFrame, and sort them in the order they appear in the DataFrame.

```
import pandas as pd
# Load toy store data into DataFrame
df = pd.read_csv("toy_store.csv")
# Count occurrences of each toy name
toy_counts = df["toy_name"].value_counts()
# Sort in original order
original_order = toy_counts.sort_index()
# Display the toy name counts
print(original_order)
```

### The output will be:

```
Action Figure 7
Barbie Doll 5
Blocks 10
Board Game 3
Dollhouse 2
Remote Control Car 6
Stuffed Animal 8
dtype: int64
```

From the output, we can see that the DataFrame has seven different types of toys, and we can see how many times each toy appears in the DataFrame in the order they appear, making it easier to get an understanding of the frequency of each toy name.

## Conclusion:

In this article, we have learned how to count unique values and sort them in different orders using Pandas.

The `value_counts()`

function in Pandas makes counting unique values and sorting them easy and efficient. By using the `sort_values()`

function, we can sort the counts in ascending or descending order, depending on the task at hand.

Additionally, we have demonstrated how we can sort counts in the order they appear in the original DataFrame. These methods can help data analysts and scientists extract valuable insights from large datasets easily.