pandas extract()

In Python, the `extract()` function is often used to extract specific patterns or matches from strings. It is implemented through regular expressions. The `extract()` function is usually called through the `Series` object of the `pandas` library, which is used to pattern match the strings in the Series and extract the matching parts.

Here's an example:

```python
import pandas as pd

data = pd.Series(["A1", "B2", "C3", "D4"])

# 使用正则表达式提取字母和数字
result = data.str.extract("([A-Z])(\d)")

print(result)
```

输出结果:
```
   0  1
0  A  1
1  B  2
2  C  3
3  D  4
```

In the above example, we extracted the alphanumeric portion of each string using the `extract()` function. The regular expression `([AZ])(\d)` is used to match a pattern of an uppercase letter followed by a number. The result of the extraction is a DataFrame containing two columns, where the first column (0) is the letter part and the second column (1) is the numeric part.

It should be noted that the `extract()` function returns a new DataFrame or Series object rather than modifying the original data.

Guess you like

Origin blog.csdn.net/m0_69097184/article/details/131969648