. Advertisement .
..3..
. Advertisement .
..4..
As in the process of inspecting the code, many of us must have encountered the situation where we have to conduct the dataframe filter by column value no less than once.
If that’s a case, here we are to get you out of trouble. Read on to learn more!
Dataframe Example Preparations
Let’s start by making a dataframe:
# Create a pandas DataFrame.
import pandas as pd
englishteaching = ({
'Teachers':["Simmon","Pinmark","Hanson","Putin","Plex","Hanson","Simmon","Putin"],
'Fee' :[32000,45000,33000,44000,36000,45000,35000,42000],
'Prolongation:['50days','30days','40days','35days','35days','60days','50days','55days'],
'Discount':[2000,1300,2000,1100,2500,2300,1200,1500]
})
df = pd.DataFrame(englishteaching, columns=['Teachers','Fee','Prolongation','Discount'])
print(df)
That way, we can achieve the below output:
Teachers Fee Prolongation Discount
0 Simmon 32000 50days 2000
1 Pinmark 45000 30days 1300
2 Hanson 33000 40days 2000
3 Putin 44000 35days 1100
4 Plex 36000 35days 2500
5 Hanson 45000 60days 2300
6 Simmon 35000 50days 1200
7 Putin 42000 55days 1500
Choose Pandas Rows From Dataframe Filter By Column Value
In this spectrum, from a DataFrame that includes or does not have the particular value for a column, we may choose pandas rows. It is frequently used to filter the DataFrame according to column value.
Choose Pandas Rows with a Particular Column Value.
- Employ Boolean indexing to filter:
When using a boolean index, we first create a mask, which is just a collection of boolean values that indicate whether or not a certain column includes a particular element.
Running the code:
df_mask=df['col_name']=='specific_value'
After that, we use this mask to filter the necessary data out of our original DataFrame.
filtered_df = df[df_mask]
This operation will give back a DataFrame that has been filtered to only show the rows with the specified value for the specified column.
import pandas as pd
teachers=['Simmon','Pinmark','Hanson','Putin','Plex','Hanson','Simmon','Putin']
fee=[32000,45000,33000,44000,36000,45000,35000,42000]
Prolongation=['50days','30days','40days','35days','35days','60days','50days','55days']
Discount=[2000,1300,2000,1100,2500,2300,1200,1500]
df = pd.DataFrame({'Teachers':teachers ,
'Fee':fee ,
'Prolongation': prolongation})
df_mask=df['Discount']==2000
filtered_df = df[df_mask]
print(filtered_df)
Output:
Teachers Fee Prolongation Discount
1 Simmon 32000 50days 2000
2 Hanson 33000 40days 2000
- Filter Utilizing Positional Indexing
Although there is an additional stage, this method is quite comparable to boolean indexing.
That way, such an approach involves making a boolean mask first, and locating the locations where the mask is True after that.
Then, we shall employ the mask’s positions with True values to provide to the iloc() method, ensuring that only the desired rows are chosen.
import pandas as pd
import numpy as np
teachers=['Simmon','Pinmark','Hanson','Putin','Plex','Hanson','Simmon','Putin']
fee=[32000,45000,33000,44000,36000,45000,35000,42000]
Prolongation=['50days','30days','40days','35days','35days','60days','50days','55days']
Discount=[2000,1300,2000,1100,2500,2300,1200,1500]
df = pd.DataFrame({'Teachers':teachers ,
'Fee':fee ,
'Prolongation': prolongation})
df_mask=df['Discount']==2000
positions = np.flatnonzero(df_mask)
filtered_df=df.iloc[positions]
print(filtered_df)
Output:
Teachers Fee Prolongation Discount
1 Simmon 32000 50days 2000
2 Hanson 33000 40days 2000
Conclusion
This instruction regarding implementing dataframe filter by column value in Pandas hopefully can provide you helpful insights.
Now, wait for no more but get on the way and idealize your coding! Wish you best of luck!
Leave a comment