【问题标题】:How to split a dataframe row into multiple dataframe row if the condition is satisfied?如果满足条件,如何将数据框行拆分为多个数据框行?
【发布时间】:2021-03-10 06:06:18
【问题描述】:

我有一个数据框

         Description

0        Hi there
1        He is my family.
2        He studies in the United States.

我想在这种情况下拆分这个数据框:如果描述超过10个字符,剩下的字符应该在下一行。

预期输出:

             Description
0            Hi there
1            He is my f
2            amily.
3            He studies
4            in the Uni
5            ted States
6            .

【问题讨论】:

    标签: python python-3.x pandas dataframe numpy


    【解决方案1】:

    Series.apply 中使用自定义 lambda function,然后在 Series.explode

    f =  lambda data: [data[x:x+10] for x in range(0, len(data), 10)]
    s = df['Description'].apply(f).explode().reset_index(drop=True)
    print (s)
    0      Hi there
    1    He is my f
    2        amily.
    3    He studies
    4     in the Un
    5    ited State
    6            s.
    Name: Description, dtype: object
    

    【讨论】:

    【解决方案2】:

    一种使用pandas.Series.str.findall的方式:

    df["Description"].str.findall(".{1,10}").explode()
    

    输出:

    0      Hi there
    1    He is my f
    1        amily.
    2    He studies
    2     in the Un
    2    ited State
    2            s.
    Name: Description, dtype: object
    

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 2023-04-09
      • 2020-10-23
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      相关资源
      最近更新 更多