There are many methods in the pandas python module that you can manipulate any dataframe easily. If you want to divide the dataframe into two parts then pandas quantile method is for you. In this entire tutorial, you will know how to implement pandas quantile with steps.
Steps to Implement Pandas Quantile
In this section, you will know all the steps required to implement pandas quantile.
Step 1: Import the required library
The first basic step is to import all the necessary libraries. In this tutorial, I am using only the pandas module so let’s import it.
import pandas as pd
Step 2: Create a Dummy Dataframe
The next step is to create a sample dataframe for the implementation. For simplicity, I am creating a simple dataframe. But you can use your own dataframe. Use the below code to create sample dataframe.
import pandas as pd
data = {"col1":[1,2,3,4,5,6],"col2":[1,10,100,100,200,300]}
df = pd.DataFrame(data)
print(df)
Output
Step 3: Apply the pandas quantile on the dataframe
Now let’s apply the pandas.quantile() method on the dataframe. Below are the examples I have compiled for you.
Example 1: Using single quantile() on dataframe
Let’s find a single quantile on the data frame. To do so you have to pass only a single quantile value. For example, I want to find the quantile value of 10% that is 0.1.
import pandas as pd
data = {"col1":[1,2,3,4,5,6],"col2":[1,10,100,100,200,300]}
df = pd.DataFrame(data)
df.quantile(.1)
Output
Example 2: Using multiple quantile() on dataframe
You can also pass more than one quantile value inside the quantile() method. It will find the quantile value for each quantile. Execute the below lines of code.
import pandas as pd
data = {"col1":[1,2,3,4,5,6],"col2":[1,10,100,100,200,300]}
df = pd.DataFrame(data)
df.quantile([.2,0.5])
Output
Example 3: Finding quantile value column-wise
You can also find the quantile value row-wise. To do so you have to pass the axis = 0 as an argument. The default axis value for this method is 0. So you will get the quantile value row-wise. Example 1 and example 2 is using the axis =0. To find the quantile value column-wise you have to use axis = 1 as an argument. Use the below lines of code to get the value.
import pandas as pd
data = {"col1":[1,2,3,4,5,6],"col2":[1,10,100,100,200,300]}
df = pd.DataFrame(data)
print(df.quantile(.1,axis=1))
Output
Conclusion
Quantile allows you to divide the dataframe into two parts. The one is valued below the quantile and the other is greater than the quantile. These are the steps for the implementation of the pandas quantile method on dataframe. I hope you have liked this tutorial. If you have any doubt 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.