Home > Enterprise >  Pandas rsplit with if contains
Pandas rsplit with if contains

Time:12-05

Using python with if contains(r'/' and r'\') and rsplit(str,1) I can separate the values. But using Pandas is not working. How can I get this result using pandas?

"PATH_IN","PATH_OUT"
"C:\USER\ARON\TESTE.TXT","C:\OUT\TESTE.TXT"
"SOUP.TXT","SOUP.TXT"
"/OPT/IN/TESTE.TXT","TESTE.TXT"

Result

"PATH_IN","NAME_IN","PATH_OUT","NAME_OUT"
"C:\USER\ARON","TESTE.TXT","C:\OUT","TESTE.TXT"
"","SOUP.TXT","","SOUP.TXT"
"/OPT/IN/","TESTE.TXT","","TESTE.TXT"

CodePudding user response:

One way would be to avoid regex and use os.path.split, since it can handle multiple separators for filenames.

df[["PATH_IN", "NAME_IN"]] = df["PATH_IN"].apply(lambda x: pd.Series(os.path.split(x)))
df[["PATH_OUT", "NAME_OUT"]] = df["PATH_OUT"].apply(lambda x: pd.Series(os.path.split(x)))

Output:

        PATH_IN PATH_OUT    NAME_IN   NAME_OUT
0  C:\USER\ARON   C:\OUT  TESTE.TXT  TESTE.TXT
1                          SOUP.TXT   SOUP.TXT
2       /OPT/IN           TESTE.TXT  TESTE.TXT

CodePudding user response:

Try this:

df['NAME_IN'] = df['PATH_IN'].str.split(r'[/\\]').str[-1]
df['NAME_OUT'] = df['PATH_OUT'].str.split(r'[/\\]').str[-1]

Output:

>>> df
                  PATH_IN          PATH_OUT    NAME_IN   NAME_OUT
0  C:\USER\ARON\TESTE.TXT  C:\OUT\TESTE.TXT  TESTE.TXT  TESTE.TXT
1                SOUP.TXT          SOUP.TXT   SOUP.TXT   SOUP.TXT
2       /OPT/IN/TESTE.TXT         TESTE.TXT  TESTE.TXT  TESTE.TXT
  • Related