如何通过拆分其中的字符串将单个列拆分为多个。 -Pandas Python [重复]

Posted

技术标签:

【中文标题】如何通过拆分其中的字符串将单个列拆分为多个。 -Pandas Python [重复]【英文标题】:How can I split a single column into multiple by splitting the string in it. -Pandas Python [duplicate] 【发布时间】:2019-06-30 04:58:08 【问题描述】:

我有一个这样的专栏:

id       col1

 1   223.237.181.234
 2  236.235.183.239
 3  247.254.167.267
 4   238.252.153.236
 5   202.237.145.252

现在我想借助列中的点 . 将这一列拆分为 4 列,如下所示:

id   col1_suffix1   col1_suffix2   col1_suffix3   col1_suffix4
1      223                237            181            234
2      236                235            183            239
3      247                254            167            267

【问题讨论】:

this answer的最后一段 【参考方案1】:
try this, 

df[['col1_suffix1','col1_suffix2','col1_suffix3','col1_suffix4']]=df['col1'].str.split('.',expand=True)

O/P:

   id             col1 col1_suffix1 col1_suffix2 col1_suffix3 col1_suffix4
0   1  223.237.181.234          223          237          181          234
1   2  236.235.183.239          236          235          183          239
2   3  247.254.167.267          247          254          167          267
3   4  238.252.153.236          238          252          153          236
4   5  202.237.145.252          202          237          145          252

【讨论】:

以上是关于如何通过拆分其中的字符串将单个列拆分为多个。 -Pandas Python [重复]的主要内容,如果未能解决你的问题,请参考以下文章