You can find out the row values for column maximal in pandas DataFrame by using DataFrame.idxmax()
, DataFrame.query()
methods and DataFrame.loc[]
property. You can also use DataFrame.nlargest()
and DataFrame.nsmallest()
to get maximum and minimum of columns.
In this article, I will explain how to find row values for column maximal of Pandas DataFrame with some examples.
1. Quick Examples of Find Rows Values for Column Maximal in Pandas
If you are in hurry below are some quick examples of how to find out row values for column maximal in pandas DataFrame.
# Below are some quick examples.
# Using DataFrame.idxmax() Method.
df2=df['Fee'].idxmax()
# Using DataFrame.loc[] property.
df2=df.loc[df['Fee'].idxmax()]
# Using DataFrame.query() method.
df2=df.query('Fee == Fee.max()')
# Using DataFrame.nlargest() function.
df2=df.nlargest(2,['Fee'])
# Using DataFrame.nsmallest() function.
df2=df.nsmallest(2,['Fee'])
Now, let’s create a Pandas DataFrame with a few rows and columns and execute some examples and validate results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# Create Pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"],
'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000],
'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days']
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
# Output:
Courses Fee Duration
0 Spark 22000 30days
1 Spark 25000 35days
2 PySpark 23000 40days
3 JAVA 24000 45days
4 Hadoop 26000 50days
5 .Net 30000 55days
6 Python 27000 60days
7 AEM 28000 35days
8 Oracle 35000 30days
9 SQL DBA 32000 40days
10 C 20000 50days
11 WebTechnologies 15000 55days
2. Using DataFrame.idxmax() Get Row Index of Column Maximal
DataFrame.idxmax() method return index of the first occurrence of maximum over requested axis (rows or columns). The idxmax()
method returns a Series with the index of the maximum value for each column. By specifying the column axis (axis='columns'
), the idxmax()
method returns a Series with the index of the maximum value for each row.
# Using DataFrame.idxmax() Method.
value=df['Fee'].idxmax()
print(value)
# Output:
# 8
This returns index 8 which contains the maximum value for a column Fee
.
3. Get the Actual Maximum Value of Column
You can get row values of column maximal of pandas by using DataFrame.loc[]
property and idxmax(). The loc[]
property is used to access a group of rows and columns by label(s) or a boolean array.
# Using DataFrame.loc[] property.
df2=df.loc[df['Fee'].idxmax()]
print(df2)
Yields below output.
# Output:
Courses Oracle
Fee 35000
Duration 30days
Name: 8, dtype: object
4. Using DataFrame.query() to Get Rows Values Column Maximal
You can get the Rows value of column maximal of pandas by using DataFrame.query()
method. The query()
method is used to query the columns of a DataFrame with a boolean expression. This returns the entire row.
# Using DataFrame.query() method.
df2=df.query('Fee == Fee.max()')
print(df2)
Yields below output.
# Output:
Courses Fee Duration
8 Oracle 35000 30days
5. Using nlargest() to Get Column Maximum
You can find the column maximum of pandas using DataFrame.nlargest()
function. The nlargest()
function is used to get the first n rows ordered by columns in descending order. The columns that are not specified are returned as well, but not used for ordering.
# Using DataFrame.nlargest() function.
df2=df.nlargest(2,['Fee'])
print(df2)
Yields below output.
# Output:
Courses Fee Duration
8 Oracle 35000 30days
9 SQL DBA 32000 40days
In the same way, you can also use it to find the minimum
values of row values of the column with DataFrame.nsmallest()
function.
# Using DataFrame.nsmallest() function.
df2=df.nsmallest(2,['Fee'])
print(df2)
Yields below output.
# Output:
Courses Fee Duration
11 WebTechnologies 15000 55days
10 C 20000 50days
6. Complete Example – Rows Values of Column Maximal
Below are complete examples of row values of column minimal of pandas DataFrame.
# Create Pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"],
'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000],
'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days']
}
df = pd.DataFrame(technologies)
print(df)
# Using DataFrame.idxmax() Method.
df2=df['Fee'].idxmax()
print(df2)
# Using DataFrame.loc[] property.
df2=df.loc[df['Fee'].idxmax()]
print(df2)
# Using DataFrame.query() method.
df2=df.query('Fee == Fee.max()')
print(df2)
# Using DataFrame.nlargest() function.
df2=df.nlargest(2,['Fee'])
print(df2)
# Using DataFrame.nsmallest() function.
df2=df.nsmallest(2,['Fee'])
print(df2)
7. Conclusion
In this article, You can find the row values to column maximal of Pandas DataFrame by using DataFrame.idxmax()
, DataFrame.query()
methods and DataFrame.loc[]
properties. You can also use DataFrame.nlargest()
and DataFrame.nsmallest()
to get maximum and minimum of columns in the DataFrame with above examples.
Related Articles
- Drop First/Last N Columns From Pandas DataFrame
- Pandas Drop Last N Rows From DataFrame
- Delete/Drop First N Rows From Pandas DataFrame
- Sort Pandas DataFrame by Single Column
- Find Unique Values From Columns in Pandas
- Find Intersection Between Two Series in Pandas?
- Pandas Convert String to Integer
- Count NaN Values in Pandas DataFrame