본문 바로가기
Python

pandas string find method (문자 찾기)

by 슈거로프 2023. 3. 25.

dataframe을 . (콤마)로 handling 가능 하다.

 

 

After you’ve loaded the data into the DataFrame, you can quickly query the whole pandas column to filter for entries that contain a substring:

>>>
>>> companies[companies.slogan.str.contains("secret")]
              company                                  slogan
7          Maggio LLC                    target secret niches
117      Kub and Sons              brand secret methodologies
654       Koss-Zulauf              syndicate secret paradigms
656      Bernier-Kihn  secretly synthesize back-end bandwidth
921      Ward-Shields               embrace secret e-commerce
945  Williamson Group             unleash secret action-items

You can use .str.contains() on a pandas column and pass it the substring as an argument to filter for rows that contain the substring.

Note: The indexing operator ([]) and attribute operator (.) offer intuitive ways of getting a single column or slice of a DataFrame.

However, if you’re working with production code that’s concerned with performance, pandas recommends using the optimized data access methods for indexing and selecting data.

When you’re working with .str.contains() and you need more complex match scenarios, you can also use regular expressions! You just need to pass a regex-compliant search pattern as the substring argument:

>>>
>>> companies[companies.slogan.str.contains(r"secret\w+")]
          company                                  slogan
656  Bernier-Kihn  secretly synthesize back-end bandwidth

 

 

출처:

 

https://realpython.com/python-string-contains-substring/

 

How to Check if a Python String Contains a Substring – Real Python

In this tutorial, you'll learn the best way to check whether a Python string contains a substring. You'll also learn about idiomatic ways to inspect the substring further, match substrings with conditions using regular expressions, and search for substring

realpython.com

 

댓글