- Related Questions & Answers
- Capitalize first letter of a column in Pandas dataframe
- Apply uppercase to a column in Pandas dataframe
- Apply function to every row in a Pandas DataFrame
- Add a row at top in pandas DataFrame
- Absolute Deviation and Absolute Mean Deviation using NumPy
- Array element with minimum sum of absolute differences?
- Arrangement of words without changing the relative position of vowel and consonants?
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- Print number of words, vowels and frequency of each character
- Absolute distinct count in a sorted array?
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array?
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Absolute difference between the first X and last X Digits of N?
- Absolute difference between sum and product of roots of a quartic equation?
- Absolute Difference of even and odd indexed elements in an Array (C++)?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In statistics, the term "frequency" indicates the number of occurrences of a value in a given data sample. As a software meant for mathematical and scientific analysis, Pandas has many in-built methods to calculate frequency from a given sample.

**Absolute Frequency** It is same as just the frequency where the number of occurrences of a data element is calculated. In the below example, we simply count the number of times the name of a city is appearing in a given DataFrame and report it out as frequency.

**Approach 1** − We use the pandas method named .value_counts.

import pandas as pd # Create Data Frame data = ["Chandigarh","Hyderabad","Pune","Pune","Chandigarh","Pune"] # use the method .value_counts() df = pd.Series(data).value_counts() print(df)

Running the above code gives us the following result:

Pune 3 Chandigarh 2 Hyderabad 1 dtype: int64

**Approach 2** − We use the pandas method named .crosstab

import pandas as pd data = ["Chandigarh","Hyderabad","Pune","Pune","Chandigarh","Pune"] df = pd.DataFrame(data,columns=["City"]) tab_result = pd.crosstab(index=df["City"],columns=["count"]) print(tab_result)

Running the above code gives us the following result:

col_0 count City Chandigarh 2 Hyderabad 1 Pune 3

**RelativeFrequency** − This is a fraction between a given frequency and the total number of observations in a data sample. So the value can be a floating point value which can also be expressed as a percentage. To find it out we first calculate the frequency as shown in the first approach and then divide it with total number of observations which is found out using the len() function.

import pandas as pd # Create Data Frame data = ["Chandigarh","Hyderabad","Pune","Pune","Chandigarh","Pune"] # use the method .value_counts() df = pd.Series(data).value_counts() print(df/len(data))

Running the above code gives us the following result:

Pune 0.500000 Chandigarh 0.333333 Hyderabad 0.166667 dtype: float64

Advertisements