【问题标题】:Splitting a long string in pandas cell near the n-th character position into multiple cells without splitting words将pandas单元格中靠近第n个字符位置的长字符串拆分为多个单元格而不拆分单词
【发布时间】:2019-10-31 14:09:39
【问题描述】:

由于 MS Excel 将单元格中的字符数限制为 32767,因此我必须将 pandas 数据框中的较长字符串拆分为多个单元格。

有没有办法将 pandas 列“Text”的字符串拆分为几列“Text_1”、“Text_2”、“Text_3”……来划分?文本块不能在一个单词中分隔也很重要,所以我认为需要正则表达式。

一个示例数据框:

df_test = pd.DataFrame({'Text' : ['This should be the first very long string','This is the second very long string','This is the third very long string','This is the last string which is very long'],
               'Date' : [2019, 2018, 2019, 2018],
               'Source' : ["FAZ", "SZ" , "HB", "HB"],
               'ID' : ["ID_1", "ID_2", "ID_3", "ID_4"]})
df_test

    Text                                        Date    Source  ID
0   This should be the first very long string   2019    FAZ     ID_1
1   This is the second very long string         2018    SZ      ID_2
2   This is the third very long string          2019    HB      ID_3
3   This is the last string which is very long  2018    HB      ID_4

假设此示例中的剪切发生在 n=15 而不是 n=32767,我想将 Text 列相应地拆分为以下内容:

    Text_1          Text_2          Text_3         Text_4      Date   Source    ID
0   This should be  the first very  long string                2019   FAZ       ID_1
1   This is the     second very     long string                2018   SZ        ID_2
2   This is the     third very long  string                    2019   HB        ID_3
3   This is the     last string     which is very  long        2018   HB        ID_4

最终,该方法应可扩展到n=32767 和至少十个新列"Text_1""Text_2" 等。

到目前为止,我已经创建了一个新列 "n",指示每行 df_text["Text"] 字符串的长度:

df_test['n'] = df_test['Text'].str.split("").str.len()

【问题讨论】:

    标签: regex python-3.x string pandas


    【解决方案1】:

    这是大意。

    # find longest long string, then divide the text 
    # into the number of new cols you want, adding a | at
    # the division and then later splitting by that |
    
    longest = ""
    for x in df_test['Text']:
        if len(x) > len(longest):
            longest = x
        continue
    
    import math 
    
    num_cols = math.floor(len(longest.split(' ')) / 3) # shoot for 3 words per row
    for index,row in df_test.iterrows():
    
        word_str = row['Text']
        word_char_len = len(word_str)
        word_as_list = word_str.split(' ')
        num_words = len(word_as_list)
    
        col_index = math.ceil(len(word_as_list) / num_cols)
    
        for _ in range(num_cols - 1):
            word_as_list.insert(col_index,'|')
            col_index += col_index
        new = ' '.join(word_as_list)
        df_test.at[index,'Text'] = new
    
    cols = ['Text'+str(i) for i in range(1,num_cols+1)]
    df_test[cols] = df_test.Text.str.split('|',expand=True)
    del df_test['Text']                                                                                                                   
    print(df_test)
    

    输出

       Date Source    ID            Text1          Text2                Text3
    0  2019    FAZ  ID_1  This should be      the first      very long string
    1  2018     SZ  ID_2     This is the    second very           long string
    2  2019     HB  ID_3     This is the     third very           long string
    3  2018     HB  ID_4     This is the    last string    which is very long
    

    完成后我会上传一个完整的。如果您不喜欢这种方式或有其他建议,请发表评论。

    【讨论】:

    • 非常感谢这个不错的解决方案!如果我知道要从长字符串创建的列数,它工作得很好。就我而言,我只知道一个单元格应包含的最大字符数(32767)。因此,我正在寻找一种可根据从长字符串创建的文本块数量进行扩展的可能性,每个文本块包含近 32767 个字符。
    • 没问题@Peet,很乐意提供帮助。您能否提供每个单元格的最大字符长度的另一种描述?这是否意味着数据框列中的单个元素的最大字符长度为 32767?
    • @Peet 我要进行编辑。要在“文本”列中找到最长的 str,您可以使用 long = df.loc[df['Text'].str.len().idxmax(), Text] 而不是我之前所做的。我刚刚对其进行了测试,它的效率略高。
    【解决方案2】:

    是的 - 单个 pandas 单元格最多应包含 32767 个字符。因此 df_test[“Text”] 中的字符串应相应拆分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 2022-01-18
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多