根据 pandas 中的分隔符复制行的一部分?

发布于 2025-01-15 00:35:29 字数 607 浏览 3 评论 0原文

是否有可能根据分隔符复制行的一部分

例如,如果我有这样的 df:

Cars             Country       Trans                     trans_id
Alto             Australia     Automatic & Manual        auto & man

我想将 & 上的每一行转换为分隔符分为 2 行并保持其他列相同

所需的输出应该是:

    Cars             Country       Trans                     trans_id        New_Trans     NewTransID
    Alto             Australia     Automatic & Manual        auto & man      Automatic     auto
    Alto             Australia     Automatic & Manual        auto & man      Manual        man

Is there any possibility to duplicate part of the row based on the delimiter

For example, if I have df like this:

Cars             Country       Trans                     trans_id
Alto             Australia     Automatic & Manual        auto & man

I want to convert each row on the & delimiter into 2 rows and keep the other columns same

The desired output should be:

    Cars             Country       Trans                     trans_id        New_Trans     NewTransID
    Alto             Australia     Automatic & Manual        auto & man      Automatic     auto
    Alto             Australia     Automatic & Manual        auto & man      Manual        man

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

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

发布评论

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

评论(1

几味少女 2025-01-22 00:35:29

使用:

df = (df.assign(new_Trans = df['Trans'].str.split(' & '),
                new_trans_id = df['trans_id'].str.split(' & '))
        .explode(['new_Trans','new_trans_id']))

Use:

df = (df.assign(new_Trans = df['Trans'].str.split(' & '),
                new_trans_id = df['trans_id'].str.split(' & '))
        .explode(['new_Trans','new_trans_id']))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文