There are many inbuilt functions in the pandas python library that you can easily manipulate dataset easily. Pandas **value_counts** is one of them. It allows you to find the counts of unique values in a series and dataframe columns. In this entire tutorial, you will know how to implement pandas value_counts with examples.

## Examples of Pandas value_counts

In this section, you will know the various examples of * pandas value_counts*. Please note that I am demonstrating the code on Python Jupyter notebook, so for better understanding compile code on it.

### Example 1: Finding unique counts in Series

Suppose you have a Series and want to know the occurrences of unique values for each element. Then you will use the function * pandas.Series.value_counts()*.

Execute the below lines of code.

```
import pandas as pd
series = pd.Series([10, 20, 10, 20, 30, 40])
print(series.value_counts())
```

**Output**

### Example 2: Find the unique count on Index

Let say I have index values of a dataframe. Then you can find the number of times index value comes in it using * value_counts() *method. Just execute the code and see the output.

```
import pandas as pd
series = pd.Index([10, 20, 10, 20, 30, 40])
print(series.value_counts())
```

**Output**

### Example 3: Counting the NaN values

If your dataset contains NaN values then also pandas * value_counts()* can find the occurrence if you use the

*argument. Firstly I will create a Sample Series with NaN and then apply*

**dropna=False***on it.*

**value_counts()**```
import pandas as pd
import numpy as np
series = pd.Series([10, 20, 10, 20, 30, np.nan])
print(series.value_counts(dropna=False))
```

**Output**

### Example 4: Applying value_counts on dataframe

The above examples were for series. In this example, you will know the implementation of pandas* value_counts* on dataframe. But before that let’s first create a Sample dataframe. After that, you will use the value_counts() method on datataframe columns.

```
import pandas as pd
df = pd.DataFrame({"col1":[10, 20, 10, 20, 30, 40],"col2":[1,2,3,1,2,4]},index=["A","B","C","D","E","F"])
print(df)
```

**Output**

Now let’s apply the value_counts on dataframe.

```
import pandas as pd
df = pd.DataFrame({"col1":[10, 20, 10, 20, 30, 40],"col2":[1,2,3,1,2,4]},index=["A","B","C","D","E","F"])
print(df["col1"].value_counts())
print(df["col2"].value_counts())
```

The above part does not have NaN values. Suppose you have NaN value in it and if you pass the * dropna=True* as an argument inside the

*function then it will remove the records for all the rows containing NaN. Just Execute the below lines of code and see the output.*

**value_counts()**```
import pandas as pd
import numpy as np
df = pd.DataFrame({"col1":[10, 20, 10, 20, np.nan, np.nan],"col2":[1,2,3,1,2,4]},
index=["A","B","C","D","E","F"])
print(df["col1"].value_counts(dropna=False))
print(df["col2"].value_counts())
```

**Output**

You can also sort the counts bypassing the * ascending = True *as an argument in the function.

```
import pandas as pd
import numpy as np
df = pd.DataFrame({"col1":[10, 20, 10, 20, np.nan, np.nan],"col2":[1,2,3,1,2,4]},
index=["A","B","C","D","E","F"])
print(df["col1"].value_counts(dropna=False))
print(df["col2"].value_counts(ascending=True))
```

**Output**

## Conclusion

The value_counts method is mostly used to find the occurrence of the unique elements in the series or dataframe. These are the examples I have aggregated for the implementation of Pandas value_counts. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

#### Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.