ডেটাফ্রেম বনাম সিরিজ
- Series (1D): একক ইনডেক্স দিয়ে ভ্যালু এক্সেস
- DataFrame (2D): রো এবং কলাম - দুটি রেফারেন্স দরকার
CSV থেকে ডেটাফ্রেম লোড
import pandas as pd
movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv")
print(type(movies)) # pandas.core.frame.DataFrame
print(movies.head(5))
কলাম সিলেকশন পদ্ধতিগুলো
১. একক কলাম
# স্ট্রিং পাস করলে Series রিটার্ন
movies["title"]
# লিস্ট পাস করলে DataFrame রিটার্ন
movies[["title"]]
২. একাধিক কলাম (লিস্ট)
cols = ["title", "runtime", "vote_average", "vote_count"]
movies[cols]
৩. .filter() মেথড (কলাম নাম দিয়ে)
# like: সাবস্ট্রিং ম্যাচ
movies.filter(like="title").head()
# regex: রেগুলার এক্সপ্রেশন
movies.filter(regex="t$").head() # 't' দিয়ে শেষ হয় এমন কলাম
# items: নির্দিষ্ট কলাম লিস্ট (KeyError দেয় না)
movies.filter(items=["title", "budget"]).head()
৪. .select_dtypes() মেথড (ডেটা টাইপ দিয়ে)
# ডেটা টাইপ চেক
print(movies.dtypes.value_counts())
# object: 5, int64: 4, float64: 3
# ইন্টিজার কলাম
movies.select_dtypes(include="int").head(3)
# ইন্টিজার + ফ্লোট
movies.select_dtypes(include=["int64", "float"]).head(3)
# সব নমেরিক (int + float)
movies.select_dtypes(include="number").head(3)
# অবজেক্ট (স্ট্রিং) বাদ দিয়ে
movies.select_dtypes(exclude="object").head(3)
মहত্বপূর্ণ নোট
- প্যান্ডাসে স্ট্রিং টাইপ নেই —
object টাইপ হিসেবে থাকে
include="string" দিলে Error: TypeError: data type "string" not understood
include="object" বা exclude="number" ব্যবহার করুন
উপসংহার
df[cols] — সরাসরি কলাম নাম দিয়ে
df.filter(like/regex/items) — কলাম নাম প্যাটার্ন দিয়ে
df.select_dtypes(include/exclude) — ডেটা টাইপ দিয়ে
- রিডেবিলিটির জন্য কলাম লিস্ট ভেরিয়েবলে রাখুন