किसी स्तंभ का अधिकतम मान ज्ञात करने और पंडों में उसके संगत पंक्ति मान वापस करने के लिए, हम df.loc[df[col].idxmax()] का उपयोग कर सकते हैं . आइए इसे बेहतर ढंग से समझने के लिए एक उदाहरण लेते हैं।
कदम
- एक द्वि-आयामी, आकार-परिवर्तनीय, संभावित रूप से विषम सारणीबद्ध डेटा बनाएं, df.
- इनपुट डेटाफ़्रेम प्रिंट करें, df.
- उस कॉलम का अधिकतम मान ज्ञात करने के लिए एक वेरिएबल, कॉलम को इनिशियलाइज़ करें।
- df.loc[df[col].idxmax()] का उपयोग करके अधिकतम मान और उसकी संगत पंक्ति का पता लगाएं
- चरण 4 आउटपुट प्रिंट करें।
उदाहरण
import pandas as pd
df = pd.DataFrame(
{
"x": [5, 2, 7, 0],
"y": [4, 7, 5, 1],
"z": [9, 3, 5, 1]
}
)
print "Input DataFrame is:\n", df
col = "x"
max_x = df.loc[df[col].idxmax()]
print "Maximum value of column ", col, " and its corresponding row values:\n", max_x
col = "y"
max_x = df.loc[df[col].idxmax()]
print "Maximum value of column ", col, " and its corresponding row values:\n", max_x
col = "z"
max_x = df.loc[df[col].idxmax()]
print "Maximum value of column ", col, " and its corresponding row values:\n", max_x आउटपुट
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 Maximum value of column x and its corresponding row values: x 7 y 5 z 5 Name: 2, dtype: int64 Maximum value of column y and its corresponding row values: x 2 y 7 z 3 Name: 1, dtype: int64 Maximum value of column z and its corresponding row values: x 5 y 4 z 9 Name: 0, dtype: int64