如何在 Pandas 中操作字符串?
Pandas 库提供了多种方法,可用于根据所需的输出操作字符串。但首先,让我们创建一个 Pandas 数据框。
Python3
import pandas as pd
data = [[1, "ABC KUMAR", "xYZ"], [2, "BCD", "XXY"],
[3, "CDE KUMAR", "ZXX"], [3, "DEF", "xYZZ"]]
cfile = pd.DataFrame(data, columns = ["SN", "FirstName", "LastName"])
cfile
Python3
# find firstname starting with 'D'
result = cfile.FirstName.str.startswith('D')
print(result)
# find lasttname containing 'XX'
result = cfile.LastName.str.contains('XX')
print(result)
# split FirstName on the basis of ' '
result = cfile.FirstName.str.split()
print(result)
# find length of lasttname
result = cfile.LastName.str.len()
print(result)
# Capitalize the first Letter of LastName
result = cfile.LastName.str.capitalize()
print(result)
# Capitalize all Letter of LastName
result = cfile.LastName.str.upper()
print(result)
# Convert all Letter of LastName to lowercase
result = cfile.LastName.str.lower()
print(result)
输出:
“Pandas”库提供了一个“ .str()”方法,可用于将数据框的任何数据创建为字符串,之后Python文档或本文中定义的任何字符串操作都可以用于该数据。
下面是说明一些示例的代码
Python3
# find firstname starting with 'D'
result = cfile.FirstName.str.startswith('D')
print(result)
# find lasttname containing 'XX'
result = cfile.LastName.str.contains('XX')
print(result)
# split FirstName on the basis of ' '
result = cfile.FirstName.str.split()
print(result)
# find length of lasttname
result = cfile.LastName.str.len()
print(result)
# Capitalize the first Letter of LastName
result = cfile.LastName.str.capitalize()
print(result)
# Capitalize all Letter of LastName
result = cfile.LastName.str.upper()
print(result)
# Convert all Letter of LastName to lowercase
result = cfile.LastName.str.lower()
print(result)
输出:
0 False
1 False
2 False
3 True
Name: FirstName, dtype: bool
0 False
1 True
2 True
3 False
Name: LastName, dtype: bool
0 [ABC, KUMAR]
1 [BCD]
2 [CDE, KUMAR]
3 [DEF]
Name: FirstName, dtype: object
0 3
1 3
2 3
3 4
Name: LastName, dtype: int64
0 Xyz
1 Xxy
2 Zxx
3 Xyzz
Name: LastName, dtype: object
0 XYZ
1 XXY
2 ZXX
3 XYZZ
Name: LastName, dtype: object
0 xyz
1 xxy
2 zxx
3 xyzz
Name: LastName, dtype: object