在 Pandas 中应用 LEFT、RIGHT、MID 的方法
很多时候,我们需要提取 Pandas 数据框中字符串中存在的特定字符。为了解决这个问题,我们在 pandas 中有 Left、Right 和 Mid 的概念。
示例 1:从左侧提取字符
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['1000-BMW','2000-Audi','3000-Volkswagen',
'4000-Datsun','5000-Toyota','6000-Maruti Suzuki']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters from right side
# using slicing and storing result in
# 'Left'
Left = df['Model_name'].str[:4]
print(Left)
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['ID-11111-BMW','ID-22222-Volkswagen',
'ID-33333-Toyota','ID-44444-Hyundai ',
'ID-55555-Datsun','ID-66666-Mercedes']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters from left side using
# slicing and storing result in 'Right'
Right = df['Model_name'].str[4:8]
print (Right)
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['ID-11111-BMW','ID-22222-Volkswagen',
'ID-33333-Toyota','ID-44444-Hyundai ',
'ID-55555-Datsun','ID-66666-Mercedes']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters from Middle using
# slicing and storing result in 'Mid'
Mid = df['Model_name'].str[4:8]
print (Mid)
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['1000-BMW','2000-Audi',
'3000-Volkswagen','4000-Datsun',
'5000-Toyota','6000-Maruti Suzuki']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters before symbol "-"
# using srt.strip() and str[0]
# and storing result to 'Before_symbol'
Before_symbol = df['Model_name'].str.split('-').str[0]
print (Before_symbol)
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['M3-1906-BMW','M5-2096-Audi',
'M11-3096-Volkswagen','M9-4096-Datsun',
'M8-5096-Toyota','M23-6096-Maruti Suzuki']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters between symbol "-"
# using srt.strip() and str[1]
# and storing result to 'Before_symbol'
BetweenTwoSymbols = df['Model_name'].str.split('-').str[1]
print (BetweenTwoSymbols)
输出 :
0 1000
1 2000
2 3000
3 4000
4 5000
5 6000
Name: Model_name, dtype: object
示例 2:从右侧提取字符
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['ID-11111-BMW','ID-22222-Volkswagen',
'ID-33333-Toyota','ID-44444-Hyundai ',
'ID-55555-Datsun','ID-66666-Mercedes']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters from left side using
# slicing and storing result in 'Right'
Right = df['Model_name'].str[4:8]
print (Right)
输出 :
0 11111
1 22222
2 33333
3 44444
4 55555
5 66666
Name: Model_name, dtype: object
示例 3:从中间提取字符
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['ID-11111-BMW','ID-22222-Volkswagen',
'ID-33333-Toyota','ID-44444-Hyundai ',
'ID-55555-Datsun','ID-66666-Mercedes']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters from Middle using
# slicing and storing result in 'Mid'
Mid = df['Model_name'].str[4:8]
print (Mid)
输出 :
0 1111
1 2222
2 3333
3 4444
4 5555
5 6666
Name: Model_name, dtype: object
示例 4:在使用str.split()函数的符号之前
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['1000-BMW','2000-Audi',
'3000-Volkswagen','4000-Datsun',
'5000-Toyota','6000-Maruti Suzuki']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters before symbol "-"
# using srt.strip() and str[0]
# and storing result to 'Before_symbol'
Before_symbol = df['Model_name'].str.split('-').str[0]
print (Before_symbol)
输出 :
0 1000
1 2000
2 3000
3 4000
4 5000
5 6000
Name: Model_name, dtype: object
示例 5:使用str.split()函数在相同符号之间
Python3
# importing pandas library
import pandas as pd
# creating and initializing a list
Cars = ['M3-1906-BMW','M5-2096-Audi',
'M11-3096-Volkswagen','M9-4096-Datsun',
'M8-5096-Toyota','M23-6096-Maruti Suzuki']
# creating a pandas dataframe
df = pd.DataFrame(Cars, columns= ['Model_name'])
# Extracting characters between symbol "-"
# using srt.strip() and str[1]
# and storing result to 'Before_symbol'
BetweenTwoSymbols = df['Model_name'].str.split('-').str[1]
print (BetweenTwoSymbols)
输出 :
0 1906
1 2096
2 3096
3 4096
4 5096
5 6096
Name: Model_name, dtype: object