我想根据定界符":"
将一列分成3个,我能够
使用下面的代码。但是现在要更改拆分的名称
默认为1,2,3,4的列。请提出建议,我该怎么做。
from pandas import *
df = DataFrame(
{'CustomerName' : ["Paul", "John"], 'Seatblocks' : ["2:218:10:4,6","2:218:10:4,6"]});
df
df.join(df.Seatblocks.apply(lambda x: Series(x.split(':'))))
python大神给出的解决方案
人们已经给出了rename
方法,但是我发现如果您避免将所有内容都塞进一行的诱惑,这些事情会更容易。有了框架后,您可以简单地分配给.columns
:
>>> sb = df.Seatblocks.str.split(":").apply(pd.Series)
>>> sb.columns = ["a", "Three Digit", "??", "coord"]
>>> pd.concat([df, sb], axis=1)
CustomerName Seatblocks a Three Digit ?? coord
0 Paul 2:218:10:4,6 2 218 10 4,6
1 John 2:218:10:4,6 2 218 10 4,6
第一行只是(df.Seatblocks.apply(lambda x: Series(x.split(':'))))
的一个版本,它利用向量化字符串操作访问器.str
(docs)的优势。