Data Mining in Python for Excel MID Function



Hey All,
I have a category column with fixed length and i want to create a new column which contain middle 3 character.

For example:
Input column: AB345NJG,ABZ26HJB
Output: “345”,“Z26”

Do we have any python function(Like MID Function in Excel) ?

Thank You.


Hi @rock_bt,

You can use the following code to get those three digits:

df['Output_column'] = df['Input_Column'].astype(str).str[2:5] 

df is the name of your dataframe
Input_Column is the name of your input column which contains AB345NJG,ABZ26HJB, etc.
Output_Column will be the new column in your data frame which will contain the three letters like 345, Z26, etc.

Hope this helps!


Thank You @ Pulkit