【发布时间】: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