পাইথন পান্ডাসে কলামের নাম থেকে কলাম সূচী পেতে, আমরা get_loc() ব্যবহার করতে পারি পদ্ধতি।
পদক্ষেপ −
- একটি দ্বি-মাত্রিক, আকার-পরিবর্তনযোগ্য, সম্ভাব্য ভিন্ন ভিন্ন ট্যাবুলার ডেটা তৈরি করুন, df .
- ইনপুট ডেটাফ্রেম প্রিন্ট করুন, df .
- ডেটাফ্রেমের কলামগুলি খুঁজুন, df.columns ব্যবহার করে .
- ধাপ ৩ থেকে কলাম প্রিন্ট করুন।
- একটি পরিবর্তনশীল শুরু করুন কলাম_নাম .
- অবস্থান পান, যেমন, কলাম_নাম-এর জন্য সূচকের .
- কলাম_নাম-এর সূচী প্রিন্ট করুন .
উদাহরণ −
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