【问题标题】:How to split a row into two or more based on a string in a column in pandas python? [duplicate]python - 如何根据pandas python中列中的字符串将一行拆分为两个或多个? [复制]
【发布时间】:2022-02-04 22:11:46
【问题描述】:

我的 pandas 数据框中有一列包含更多信息。我需要根据列中值的标点符号将其拆分为不同的行。谁能帮我解决这个问题。

我有什么:

ProductId   Icecream                  State    Country
1           Vanilla;Strawberry        TN       IN
2           Blueberry;Vanilla;Mango   KL       IN

预期输出:

ProductId   Icecream                  State    Country
1           Vanilla                   TN       IN
1           Strawberry                TN       IN
2           Blueberry                 KL       IN
2           Vanilla                   KL       IN
2           Mango                     KL       IN

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    使用str.splitexplode

    >>> df1.assign(Icecream=df1['Icecream'].str.split(';')).explode('Icecream')
    
       ProductId    Icecream State Country
    0          1     Vanilla    TN      IN
    0          1  Strawberry    TN      IN
    1          2   Blueberry    KL      IN
    1          2     Vanilla    KL      IN
    1          2       Mango    KL      IN
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-30
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多