英文:
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 = {'Column 1': ['a', 'b', 'c'],
'id': [2468938493843983, 345642232, 23343433]}
df = pd.DataFrame(data)
df['id'] = df['id'].astype(str)
df['fromleft'] = [','.join([df['id'][i][j:j+6] for j in range(0, len(df['id'][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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论