考虑一个列为path
的数据帧df
/home/dir1/myfile1.txt
/home/anotherDir2/myfile2.txt
/home/anotherDir3/AnotherMyfile3.txt
我想为每一行拆分文件夹和文件名部分。你知道吗
我知道
df.path.str.rfind('/')
给我整个系列的最后一个索引/
。我想将这个索引结果分别应用于每一行,但是
df.path.str.slice(0, df.path.str.rfind('/'))
返回所有NA
。似乎slice
期望单个整数参数为endposition
,而不是序列。你知道吗
在python中如何实现这一点?你知道吗
这是使用的数据帧:
您可以使用
apply()
遍历df
行并提取最后一个'/'
之前的所有内容:返回:
同样,您也可以这样做来提取最后一个
'/'
之后的所有内容:返回:
如果要同时获取文件夹和文件,可以使用这样的函数,将字符串按
'/'
拆分并返回最后2个元素:然后您可以
apply()
它并将两列添加到您的df中:返回:
相关问题 更多 >
编程相关推荐