8 ways to apply LEFT, RIGHT, MID in Pandas

At times, you may need to extract specific characters within a string. You may then apply the concepts of Left, Right, and Mid in pandas to obtain your desired characters within a string.

In this tutorial, I’ll review the following 8 scenarios to explain how to extract specific characters:

(1) From the left
(2) From the right
(3) From the middle
(4) Before a symbol
(5) Before space
(6) After a symbol
(7) Between identical symbols
(8) Between different symbols

Reviewing LEFT, RIGHT, MID in Pandas

For each of the above scenarios, the goal is to extract only the digits within the string. For example, for the string of ‘55555-abc‘ the goal is to extract only the digits of 55555.

Let’s now review the first case of obtaining only the digits from the left.

Scenario 1: Extract Characters From the Left

Suppose that you have the following 3 strings:

 

Identifier
55555-abc
77777-xyz
99999-mmm

 

You can capture those strings in Python using pandas DataFrame.

Since you’re only interested to extract the five digits from the left, you may then apply the syntax of str[:5] to the ‘Identifier’ column:

 

from pandas import DataFrame

Data = {'Identifier': ['55555-abc','77777-xyz','99999-mmm']}
df = DataFrame(Data, columns= ['Identifier'])
Left = df['Identifier'].str[:5]

print (Left)

 

Once you run the Python code, you’ll get only the digits from the left:

8 ways to apply LEFT, RIGHT, MID in Pandas

Scenario 2: Extract Characters From the Right

In this scenario, the goal is to get the five digits from the right:

 

Identifier
ID-55555
ID-77777
ID-99999

 

To accomplish this goal, apply str[-5:] to the ‘Identifier’ column:

 

from pandas import DataFrame

Data = {'Identifier': ['ID-55555','ID-77777','ID-99999']}
df = DataFrame(Data, columns= ['Identifier'])
Right = df['Identifier'].str[-5:]

print (Right)

 

This will ensure that you’ll get the five digits from the right:

8 ways to apply LEFT, RIGHT, MID in Pandas

Scenario 3: Extract Characters From the Middle

There are cases where you’d like to extract the information from the middle of a string:

 

Identifier
ID-55555-End
ID-77777-End
ID-99999-End

 

To extract only the digits from the middle, you’ll need to specify the starting and ending points for your desired characters. In this case, the starting point is ‘3’ while the ending point is ‘8’ so that you’ll need to apply str[3:8] like this:

 

from pandas import DataFrame

Data = {'Identifier': ['ID-55555-End','ID-77777-End','ID-99999-End']}
df = DataFrame(Data, columns= ['Identifier'])
Mid = df['Identifier'].str[3:8]

print (Mid)

 

Only the five digits within the middle of the string will be retrieved:

8 ways to apply LEFT, RIGHT, MID in Pandas

Scenario 4: Before a symbol

Say that you want to obtain all the digits before the dash symbol (‘-‘):

 

Identifier
111-IDAA
2222222-IDB
33-IDCCC

 

Even if your string length changes, you can still retrieve all the digits from the left by adding the two components below:

  • str.split(‘-‘) – where you should place the symbol within the brackets. In our case, it is the dash symbol
  • str[0] – where you should place 0 to get the characters from the left

And here is the complete Python code:

 

from pandas import DataFrame

Data = {'Identifier': ['111-IDAA','2222222-IDB','33-IDCCC']}
df = DataFrame(Data, columns= ['Identifier'])
BeforeSymbol = df['Identifier'].str.split('-').str[0]

print (BeforeSymbol)

 

And the result:

LEFT, RIGHT, MID in Pandas

Scenario 5: Before space

What if you have a space within the string?

 

Identifier
111 IDAA
2222222 IDB
33 IDCCC

 

In that case, simply leave a blank space within the split: str.split(‘ ‘)

 

from pandas import DataFrame

Data = {'Identifier': ['111 IDAA','2222222 IDB','33 IDCCC']}
df = DataFrame(Data, columns= ['Identifier'])
BeforeSpace = df['Identifier'].str.split(' ').str[0]

print (BeforeSpace)

 

Only the digits from the left will be obtained:

LEFT, RIGHT, MID in Pandas

Scenario 6: After a symbol

You may also face situations where you’d like to get all the characters after a symbol (the dash symbol for example) for a varying-length strings:

 

Identifier
IDAA-111
IDB-2222222
IDCCC-33

 

In this case, you’ll need to adjust the value within the str[] to 1, so that you’ll obtain the desired digits from the right:

 

from pandas import DataFrame

Data = {'Identifier': ['IDAA-111','IDB-2222222','IDCCC-33']}
df = DataFrame(Data, columns= ['Identifier'])
AfterSymbol = df['Identifier'].str.split('-').str[1]

print (AfterSymbol)

 

Here is the output from Python:

LEFT, RIGHT, MID in Pandas

Scenario 7: Between identical symbols

Now what if you want to retrieve the values between two identical symbols (the dash symbols) for varying-length strings:

 

Identifier
IDAA-111-AA
IDB-2222222-B
IDCCC-33-CCC

 

In that case, set:

  • str.split(‘-‘)
  • str[1]

So your full Python code would look like this:

 

from pandas import DataFrame

Data = {'Identifier': ['IDAA-111-AA','IDB-2222222-B','IDCCC-33-CCC']}
df = DataFrame(Data, columns= ['Identifier'])
BetweenTwoSymbols = df['Identifier'].str.split('-').str[1]

print (BetweenTwoSymbols)

 

You’ll get all the digits between the two dash symbols:

LEFT, RIGHT, MID in Pandas

Scenario 8: Between different symbols

For the final scenario, the goal is to obtain the digits between two different symbols (the dash symbol and the dollar symbol):

 

Identifier
IDAA-111$AA
IDB-2222222$B
IDCCC-33$CCC

 

To accomplish this goal:

  • First, set the variable (i.e., betweenTwoDifferentSymbols) to obtain all the characters after the dash symbol
  • Then, set the same variable to obtain all the characters before the dollar symbol

This is how you code would look like:

 

from pandas import DataFrame

Data = {'Identifier': ['IDAA-111$AA','IDB-2222222$B','IDCCC-33$CCC']}
df = DataFrame(Data, columns= ['Identifier'])
betweenTwoDifferentSymbols = df['Identifier'].str.split('-').str[1]
betweenTwoDifferentSymbols = betweenTwoDifferentSymbols.str.split('$').str[0]

print (betweenTwoDifferentSymbols)

 

And the result:

LEFT, RIGHT, MID in Pandas

Conclusion – LEFT, RIGHT, MID in Pandas

You just saw how to apply Left, Right, Mid in Pandas. The concepts reviewed in this tutorial can be applied across large number of different scenarios.

The application of string functions is quite popular in Excel. Yet, you can certainly use pandas to accomplish the same goal in an easy manner.

You can find many examples about working with text data by visiting the Pandas Documentation.