String की Cleaning
.str accessor एक साथ पूरे column पर string methods apply करता है।
In this page:
Syntax
df["column"].str.lower()
df["column"].str.strip()
df["column"].str.contains("text")
String Cleaning
df["col"].str के जरिए string methods access करें: lower, upper, strip, contains, startswith, len, और बाकी। ये हर value पर काम करते हैं और missing वालों को skip करते हैं। Regular expressions contains, replace, और extract में support हैं।
Note:
str.title() हर word capitalize करता है, names के लिए handy।
उदाहरण: String cleaning
import pandas as pd
s = pd.Series([" alice SMITH ", "BOB jones", None])
print(s.str.strip().str.title())
print(s.str.contains("bob", case=False))
print(s.str.len())
# Output:
# 0 Alice Smith
# 1 Bob Jones
# 2 None
# dtype: object
# 0 False
# 1 True
# 2 None
# dtype: object
# 0 14.0
# 1 9.0
# 2 NaN
# dtype: float64
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- सीधे Series पर string methods call करना
- text columns में NaN ignore करना
- यह भूल जाना कि str methods नई Series return करते हैं
चैप्टर सारांश
- .str accessor इस्तेमाल करें
- Methods हर value पर चलते हैं
- NaN safely skip होता है
- Regex support है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: