I have a file path location: file df = /a/b/c/d/e/f/g/h/i/j/k/l/m/n/a.c /a/b/c/d/e/x/b.c I am using the below logic to remove redundant path df["file"]= df["file"].str.extract(r"(?:/[\w\.-]+){7}/(.+)") My output is df = /a/b/c/d/e/f/g/... /a/b/c/d/e/x/b.c I am interested to display the file name a.c completely and have output to show the accurate location of a.c with atleast last 3 subdirectory and first few such as 5 subdirectories. /a/b/c/d/..../l/m/n/a.c How can we parse correctly ? Continue reading...