如何在数据框列的每6个字符串中拆分字符串?

huangapple go评论61阅读模式
英文:

How to split string in every 6th strings which are the subsets of a dataframe column?

问题

在一个数据框的列中,我想将格式为字符串的子集数据拆分成每6位数字,并添加逗号',',以便我可以获得该列下的HS编码列表。我尝试了以下方法,但需要进行一些修正:

df.loc[df[:, 1] for i in range(0, len(['id']), 6)

请注意,上述代码中可能存在语法错误和逻辑错误,需要进行修正。

英文:

In a dataframe column, I would like to split subset data in format of strings into every 6 digits and add a comma ',' so that I can get a list of hs codes under the column. I tried the below but it needs some correction.

df.loc[df[:, 1] for i in range(0, len(['id'], 6)

答案1

得分: 0

假设你想从左边分割:

df['id'] = df['id'].astype(str).str.replace(r'(.{6})(?=.)', r'\1,', regex=True)

输出:
                 id

0 280530,284442,284690


<details>
<summary>英文:</summary>

Assuming you want to split from the left:

df['id'] = df['id'].astype(str).str.replace(r'(.{6})(?=.)', r'\1,', regex=True)

Output:
                 id

0 280530,284442,284690


</details>



# 答案2
**得分**: 0

**输出:**

```plaintext
      Column 1                id            fromleft
    0        a  2468938493843983  246893,849384,3983
    1        b         345642232          345642,232
    2        c          23343433           233434,33
英文:

Code:

import pandas as pd

data = {&#39;Column 1&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;],
        &#39;id&#39;: [2468938493843983, 345642232, 23343433]}

df = pd.DataFrame(data)

df[&#39;id&#39;] = df[&#39;id&#39;].astype(str)
df[&#39;fromleft&#39;] = [&#39;,&#39;.join([df[&#39;id&#39;][i][j:j+6] for j in range(0, len(df[&#39;id&#39;][i]), 6)]) for i in range(len(df))]

print(df)

Output:

  Column 1                id            fromleft
0        a  2468938493843983  246893,849384,3983
1        b         345642232          345642,232
2        c          23343433           233434,33

huangapple
  • 本文由 发表于 2023年2月10日 12:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定