datafraeme中的带状路径熊猫

发布于 2025-02-13 20:35:45 字数 831 浏览 0 评论 0原文

我在pandas中有此数据框,

     path                                               variable             
0   /home/opt/chat                                          A                      
1   /home/test                                              B                   
2   /home/opt/projects                                      C
3   /home/opt/projects/ex/remove/results                    D

如何将列路径剥离以在我的数据框架中返回此输出?

     path                                               variable             
0    chat                                                 A                      
1    test                                                 B                     
2    projects                                             C
3    results                                              D

I have this dataframe in pandas

     path                                               variable             
0   /home/opt/chat                                          A                      
1   /home/test                                              B                   
2   /home/opt/projects                                      C
3   /home/opt/projects/ex/remove/results                    D

How do I strip the column path to return this output in my dataframe?

     path                                               variable             
0    chat                                                 A                      
1    test                                                 B                     
2    projects                                             C
3    results                                              D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2025-02-20 20:35:46

您可以使用“ nofollow noreferrer”> /a>带有n = 1,以限制为一个拆分:

df['path'] = df['path'].str.rsplit('/', n=1).str[-1]

或者,也许更好, str.STR.STR.EXTRACT带有简短的正则([^/]+$) [^/]+,就在行结束之前$):

df['path'] = df['path'].str.extract(r'([^/]+$)', expand=False)

输出:

       path variable
0      chat        A
1      test        B
2  projects        C
3   results        D

You can use rsplit with n=1, to limit to one split:

df['path'] = df['path'].str.rsplit('/', n=1).str[-1]

or, maybe better, str.extract with a short regex ([^/]+$) (any series of non-/ characters [^/]+, just before the end of line $):

df['path'] = df['path'].str.extract(r'([^/]+$)', expand=False)

output:

       path variable
0      chat        A
1      test        B
2  projects        C
3   results        D
半衾梦 2025-02-20 20:35:46

使用 series.str。 Split wth通过索引选择最后一个值:

df['path'] = df['path'].astype(str).str.split('/').str[-1]
print (df)
       path variable
0      chat        A
1      test        B
2  projects        C
3   results        D

Use Series.str.split wth select last value by indexing:

df['path'] = df['path'].astype(str).str.split('/').str[-1]
print (df)
       path variable
0      chat        A
1      test        B
2  projects        C
3   results        D
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文