【问题标题】:Issue while splitting pandas dataframe column into n columns将 pandas 数据框列拆分为 n 列时出现问题
【发布时间】:2019-09-03 18:46:18
【问题描述】:

我有一个数据框,下面的列 Title。每个句子重复三遍。我想平均分成三列。

    Title
0  [1.3] Avg ticket size - merchant vs industry and benchamark (processed data).[1.3] Avg ticket size - merchant vs industry and benchamark (processed data).[1.3] Avg ticket size - merchant vs industry and benchamark (processed data)
1  [10.1] Overall portfolio and benchmarks for the bank over the last 5 quarters.[10.1] Overall portfolio and benchmarks for the bank over the last 5 quarters.[10.1] Overall portfolio and benchmarks for the bank over the last 5 quarters
2  [10.10] Decline Reasons (Quarter-wise)- E-Com vs. POS and comparison with benchmark.[10.10] Decline Reasons (Quarter-wise)- E-Com vs. POS and comparison with benchmark.[10.10] Decline Reasons (Quarter-wise)- E-Com vs. POS and comparison with benchmark
3  [10.2] QoQ Pct of Transaction Method Spend, Transaction Method Active Cards.[10.2] QoQ Pct of Transaction Method Spend, Transaction Method Active Cards.[10.2] QoQ Pct of Transaction Method Spend, Transaction Method Active Cards
4  [10.3] Pct of Transaction Method Spend, Transaction Method Active Cards by Product Type.[10.3] Pct of Transaction Method Spend, Transaction Method Active Cards by Product Type.[10.3] Pct of Transaction Method Spend, Transaction Method Active Cards by Product Type
5  [10.4] QoQ of average ticket size, transactions per card, average spend per card.[10.4] QoQ of average ticket size, transactions per card, average spend per card.[10.4] QoQ of average ticket size, transactions per card, average spend per card

我尝试了下面的代码,但没有按预期工作。

import textwrap
pd.DataFrame([textwrap.wrap(el, len(el)//3) for el in df['Title']]).add_prefix('Title') 

我想通过查找字符串的长度进行拆分,然后根据 len(string)/3 进行拆分。因为有时会有。在句子中间

请帮忙

【问题讨论】:

  • 在你的其他帖子中使用 anky 的解决方案,但使用np.array_split 而不是np.split
  • split by len(string)/3 永远不会将您的字符串分成 3 个完整的句子。原因是每个字符串都重复了 3 次,但您添加了空格和句点标记 '.' 给它们。因此,len(repeated-sentence) 不等于 3 个完整句子的 len 之和。
  • 我遇到了问题。我刚刚通过将 1 添加到 len 变量来修改“yatu”解决方案。该解决方案对我有用。

标签: python string pandas split


【解决方案1】:

看起来你可以在这里使用str.split

df.Title.str.split(r'\[\d+\.\d+\]\s', expand=True)

0                                                  1  \
0    Avg ticket size - merchant vs industry and ben...   
1    Overall portfolio and benchmarks for the bank ...   
2    Decline Reasons (Quarter-wise)- E-Com vs. POS ...   
3    QoQ Pct of Transaction Method Spend, Transacti...   
4    Pct of Transaction Method Spend, Transaction M...   
5    QoQ of average ticket size, transactions per c...   

                                                   2  \
0  Avg ticket size - merchant vs industry and ben...   
1  Overall portfolio and benchmarks for the bank ...   
2  Decline Reasons (Quarter-wise)- E-Com vs. POS ...   
3  QoQ Pct of Transaction Method Spend, Transacti...   
4  Pct of Transaction Method Spend, Transaction M...   
5  QoQ of average ticket size, transactions per c...   

                                                   3  
0  Avg ticket size - merchant vs industry and ben...  
1  Overall portfolio and benchmarks for the bank ...  
2  Decline Reasons (Quarter-wise)- E-Com vs. POS ...  
3  QoQ Pct of Transaction Method Spend, Transacti...  
4  Pct of Transaction Method Spend, Transaction M...  
5  QoQ of average ticket size, transactions per c...  

更新

如果您想查找每行中字符串的长度并将其拆分为 3,则一种方法可能是:

n = 3
lens = df.Title.str.len()//n
l = [[i[(c-1)*sl:c*sl] for i, sl in zip(df.Title, lens)] for c in range(1, n+1)]
pd.DataFrame.from_records(l).T

                                0  \
0  [1.3] Avg ticket size - merchant vs industry a...   
1  [10.1] Overall portfolio and benchmarks for th...   
2  [10.10] Decline Reasons (Quarter-wise)- E-Com ...   
3  [10.2] QoQ Pct of Transaction Method Spend, Tr...   
4  [10.3] Pct of Transaction Method Spend, Transa...   
5  [10.4] QoQ of average ticket size, transaction...   

                                                   1  \
0  .[1.3] Avg ticket size - merchant vs industry ...   
1  .[10.1] Overall portfolio and benchmarks for t...   
2  .[10.10] Decline Reasons (Quarter-wise)- E-Com...   
3  .[10.2] QoQ Pct of Transaction Method Spend, T...   
4  .[10.3] Pct of Transaction Method Spend, Trans...   
5  .[10.4] QoQ of average ticket size, transactio...   

                                                   2  
0  ).[1.3] Avg ticket size - merchant vs industry...  
1  s.[10.1] Overall portfolio and benchmarks for ...  
2  k.[10.10] Decline Reasons (Quarter-wise)- E-Co...  
3  s.[10.2] QoQ Pct of Transaction Method Spend, ...  
4  e.[10.3] Pct of Transaction Method Spend, Tran...  
5  d.[10.4] QoQ of average ticket size, transacti...  

【讨论】:

  • @yatu.是否可以根据len(string)/3求字符串的长度,然后拆分。因为有时候句子中间会有.
  • 更新@Arvinth
【解决方案2】:

您可以使用正则表达式来拆分字符串,而不是将长度除以 3。

注意,这样的分割模式可以定义为:

r'\.(?=\[)'

即:

  • \. - 一个(字面)点,
  • (?=\[\d+\.\d+]) - 后跟 [,一个数字序列,另一个点, 另一个数字序列和]

注意第二部分是一个正向预测,所以它不会是 匹配的一部分,这个 [nn,mm] 片段将是开始部分 下一个字符串的(拆分结果)。

你没有写关于删除这个 [nn,mm] 片段,所以它应该 留在分裂的结果中,这是另一个的缺点 回答。

所以生成 df 并与 3 个新列连接的代码可以是:

df2 = pd.concat([df, df.Title.str.split(
    r'\.(?=\[\d+\.\d+])', expand=True)], axis=1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多