Skip to content
Related Articles

Related Articles

Ways to apply LEFT, RIGHT, MID in Pandas

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jul, 2020
View Discussion
Improve Article
Save Article

Many times we need to extract specific characters present within a string in Pandas data frame. In order to solve this issue, we have concept of Left, Right, and Mid in pandas.

Example 1: Extract Characters From the Left

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)

Output :

0    1000
1    2000
2    3000
3    4000
4    5000
5    6000
Name: Model_name, dtype: object

Example 2: Extract Characters From the 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 left side using
# slicing and storing result in 'Right'
Right = df['Model_name'].str[4:8]
  
print (Right)

Output :

0    11111
1    22222
2    33333
3    44444
4    55555
5    66666
Name: Model_name, dtype: object

Example 3: Extract Characters From the Middle

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)

Output :

0    1111
1    2222
2    3333
3    4444
4    5555
5    6666
Name: Model_name, dtype: object

Example 4 : Before a symbol using str.split() function

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)

Output :

0    1000
1    2000
2    3000
3    4000
4    5000
5    6000
Name: Model_name, dtype: object

Example 5 :  Between identical symbols using str.split() function

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)

Output :

0    1906
1    2096
2    3096
3    4096
4    5096
5    6096
Name: Model_name, dtype: object

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!