原创博文,转载请注明出处!
series中的元素均为字符串时,通过str.split可将字符串按指定的分隔符拆分成若干列的形式。
例子:
拆分以逗号为分隔符的字符串
1 # -*- coding: utf-8 -*-
2 # 创建dataframe
3 import pandas as pd
4 s = pd.DataFrame(['a,b,c','c,d,e'])
5 print(s)
6 """
7 0
8 0 a,b,c
9 1 c,d,e
10 """
11 # 字符串拆分--expend = False
12 temp_expend_False = s[0].str.split(',')
13 print(temp_expend_False)
14 """
15 0 [a, b, c]
16 1 [c, d, e]
17 """
18
19 # 字符串拆分--expend = True
20 temp_expend_True = s[0].str.split(',',expand = True)
21 print(temp_expend_True)
22 """
23 0 1 2
24 0 a b c
25 1 c d e
26 """