पायथन पांडा में कॉलम नाम से कॉलम इंडेक्स प्राप्त करने के लिए, हम get_loc() . का उपयोग कर सकते हैं विधि।
कदम -
- एक द्वि-आयामी, आकार-परिवर्तनीय, संभावित रूप से विषम सारणीबद्ध डेटा बनाएं, df ।
- इनपुट डेटाफ़्रेम प्रिंट करें, df ।
- df.columns . का उपयोग करके DataFrame के कॉलम ढूंढें ।
- चरण 3 से कॉलम प्रिंट करें।
- एक चर प्रारंभ करें column_name ।
- स्थान प्राप्त करें, अर्थात column_name . के लिए अनुक्रमणिका का स्थान प्राप्त करें ।
- कॉलम_नाम की अनुक्रमणिका प्रिंट करें ।
उदाहरण -
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 1 is:\n", df
columns = df.columns
print"Columns in the given DataFrame: ", columns
column_name = "z"
column_index = columns.get_loc(column_name)
print"Index of the column ", column_name, " is: ", column_index
column_name = "x"
column_index = columns.get_loc(column_name)
print"Index of the column ", column_name, " is: ", column_index
column_name = "y"
column_index = columns.get_loc(column_name)
print"Index of the column ", column_name, " is: ", column_index आउटपुट
Input DataFrame 1 is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 Columns in the given DataFrame: Index(['x', 'y', 'z'], dtype='object') Index of the column z is: 2 Index of the column x is: 0 Index of the column y is: 1