Pandas-এ দুটি কলামের মধ্যে করিলেশন বের করতে আমরা .corr() মেথড ব্যবহার করতে পারি। একটা উদাহরণ নিই এবং দেখি কীভাবে এই মেথডটি প্রয়োগ করতে হয়।
ধাপসমূহ
- একটি дву-মাত্রিক, সাইজ-মিউটেবল, পোটেনশিয়ালি হেটেরোজেনাস ট্যাবুলার ডেটা, df তৈরি করুন।
- ইনপুট DataFrame, df প্রিন্ট করুন।
- দুটি ভেরিয়েবল, col1 এবং col2 আরম্ভ করুন, এবং সেগুলোতে আপনি যে কলামগুলোর করিলেশন বের করতে চান সেগুলো অ্যাসাইন করুন।
- df[col1].corr(df[col2]) ব্যবহার করে col1 এবং col2-এর মধ্যে করিলেশন বের করুন এবং করিলেশন মানটি corr ভেরিয়েবলে সংরক্ষণ করুন।
- করিলেশন মান, corr প্রিন্ট করুন।
উদাহরণ
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)
col1, col2 = "x", "y"
corr = df[col1].corr(df[col2])
print("Correlation between ", col1, " and ", col2, "is: ", round(corr, 2))
col1, col2 = "x", "x"
corr = df[col1].corr(df[col2])
print("Correlation between ", col1, " and ", col2, "is: ", round(corr, 2))
col1, col2 = "x", "z"
corr = df[col1].corr(df[col2])
print("Correlation between ", col1, " and ", col2, "is: ", round(corr, 2))
col1, col2 = "y", "x"
corr = df[col1].corr(df[col2])
print("Correlation between ", col1, " and ", col2, "is: ", round(corr, 2))
আউটপুট
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 Correlation between x and y is: 0.41 Correlation between x and x is: 1.0 Correlation between x and z is: 0.72 Correlation between y and x is: 0.41