[英]Pandas split column in multiple columns
I have a dataframe that contains in the column "Title" the following information:我有一个 dataframe 在“标题”列中包含以下信息:
Title
2.1 text1 and bla bla 1
2.2 text2 bla 2
2.3 text3 other text 7
I tried to split the column in the following manner:我尝试按以下方式拆分列:
col1 Title col3
2.1 text1 and bla bla 1
2.2 text2 bla 2
2.3 text3 other text 7
I tried to follow multiple ways but without success.我试图遵循多种方式,但没有成功。
You can split on white space to obtain x
.您可以在空白处拆分以获得
x
。 Then take the first items of lists and assign them to 'col1'
and the last items to 'col3'
.然后取出列表的第一项并将它们分配给
'col1'
和最后一项分配给'col3'
。 Finally take what's left, join them back and assign them to 'Title'
:最后拿走剩下的,加入他们并将他们分配给
'Title'
:
x = df['Title'].str.split()
df['col1'] = x.str[0]
df['col3'] = x.str[-1]
df['Title'] = x.apply(lambda y: ' '.join(y[1:-1]))
df = df[['col1','Title','col3']]
Output: Output:
col1 Title col3
0 2.1 text1 and bla bla 1
1 2.2 text2 bla 2
2 2.3 text3 other text 7
@Neither gives a much better solution in the comments. @Neither 在评论中给出了更好的解决方案。
df.Title.str.split(r"^(\d\.\d)\s(.+)\s(\d)$", expand=True).iloc[:, 1:-1].add_prefix("col_")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.