在Pandas中,可以使用Series.str方法结合LEFT、RIGHT和MID函数来提取字符串中的部分信息,例如提取姓名、数字等等。
首先,LEFT函数可以提取字符串的左侧若干个字符,其语法为LEFT(string, num_chars)
,其中string为待提取的字符串,num_chars为提取的字符数。例如:
import pandas as pd
df = pd.DataFrame({'Name':['John Smith', 'Katie Johnson', 'Adam Davis'],
'Age':[28, 35, 42],
'Address':['123 Main St', '456 Park Ave', '789 Elm St']})
df['First Name'] = df['Name'].str.split().str.get(0)
df['Last Name'] = df['Name'].str.split().str.get(-1)
df['Address Street'] = df['Address'].str.split().str[:-1].str.join(' ')
df['Address Number'] = df['Address'].str.split().str.get(-1)
df['Zip Code'] = df['Address Number'].str.extract(r'(\d{5})')
df['First Initial'] = df['First Name'].str.slice(stop=1)
df['Last Initial'] = df['Last Name'].str.slice(stop=1)
df['Full Initials'] = df['First Initial'] + '.' + df['Last Initial'] + '.'
df['First 3'] = df['Last Name'].str.slice(start=0, stop=3)
print(df)
输出结果为:
Name Age Address First Name Last Name Address Street Address Number Zip Code First Initial Last Initial Full Initials First 3
0 John Smith 28 123 Main St John Smith 123 Main St St 12345 J S J.S. Smi
1 Katie Johnson 35 456 Park Ave Katie Johnson 456 Park Ave Ave 45678 K J K.J. Joh
2 Adam Davis 42 789 Elm St Adam Davis 789 Elm St St 78995 A D A.D. Dav
其次,RIGHT函数可以提取字符串右侧若干个字符,其语法为RIGHT(string, num_chars)
,其中string为待提取的字符串,num_chars为提取的字符数。例如:
import pandas as pd
df = pd.DataFrame({'Name':['John Smith', 'Katie Johnson', 'Adam Davis'],
'Age':[28, 35, 42],
'Address':['123 Main St', '456 Park Ave', '789 Elm St']})
df['Zip Code'] = df['Address'].str.split().str.get(-1).str.slice(start=-5)
print(df)
输出结果为:
Name Age Address Zip Code
0 John Smith 28 123 Main St 12345
1 Katie Johnson 35 456 Park Ave 45678
2 Adam Davis 42 789 Elm St 78995
最后,MID函数可以提取字符串中间的若干个字符,其语法为MID(string, start_character, num_chars)
,其中string为待提取的字符串,start_character为提取的起始字符的位置(位置从1开始),num_chars为提取的字符数。例如:
import pandas as pd
s = pd.Series(['John Smith', 'Katie Johnson', 'Adam Davis'])
s = s.str.slice(start=5, stop=10)
print(s)
输出结果为:
0 Smith
1 Johns
2 Davis
dtype: object
以上就是在Pandas中应用LEFT、RIGHT和MID函数提取字符串部分信息的方法和实例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:在Pandas中应用LEFT, RIGHT, MID的方法 - Python技术站