Python – Select the middle part of the string to the new column pandas based on the total str length

Select the middle part of the string to the new column pandas based on the total str length… here is a solution to the problem.

Select the middle part of the string to the new column pandas based on the total str length

How do I extract the year part of a string based on dynamically changing length

Sample data

Name
C:/Users/Documents/Ownership_1_Year_2015.csv
C:/Users/Documents/Non_Ownersip_1_Year_2016.csv
C:/Users/Documents/Ownership_1_Year_2016.csv

The formula I’m playing with is

df['Name_Stripped'] = df['Name'].str[:-4]

This obviously removes the .csv, I tried len(str)-8 would provide a dynamic starting value, but not surprisingly, it was confused by the length I was trying to calculate. I do think the for loop can work with the length of x per call, but I’m sure there’s more pythonic way I don’t know…

Expected Results:

 Name
2015
2016
2016

Solution

Is this a solution?

 df['Name_Stripped'] = df['Name'].str[-8:-4]

Output:

                                              Name Name_Stripped
0     C:/Users/Documents/Ownership_1_Year_2015.csv          2015
1  C:/Users/Documents/Non_Ownersip_1_Year_2016.csv          2016
2     C:/Users/Documents/Ownership_1_Year_2016.csv          2016

Related Problems and Solutions