如何从数据框中提取folder \ filename.txt
我的数据框:
C:\folder\one\file.txt
C:\folder\subfolder\two\file2.txt
我需要输出最后一个文件夹和文件名:
df:
one\file.txt
two\file2.txt
我的代码:
df[0] = df[0].apply(lambda x: x.split('\\')[-1]) # i am receiving only file.txt - only filename , not last folder and filename
答案 0 :(得分:1)
稍微修改您的通话:
import os
df[0] = df[0].apply(lambda x: os.sep.join(x.split('\\')[-2:])))
在这里,os.sep
是系统分隔符,它使呼叫系统独立。您也可以使用其他任何字符串。
答案 1 :(得分:0)
尝试:
# This function takes in input a list and converts it into a dir chain string
def convert(string_list):
string = ""
# traversing each element of the list and using it to create a new string
for x in string_list:
string += x + "\\"
# returning string[:-1] to get rid of the redundant \ in the end of the string (not advised when location is of a directory)
return string[:-1]
a = r"C:\folder\one\file.txt"
print(convert(a.split("\\")[-2:]))
输出:
one\file.txt