3 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
你可以只使用split:
pd.DataFrame([y.split(':') for y in x], columns = ['Name','Age', 'Occupation'])
輸出:
Name Age Occupation
0 john 42 engineer
1 michael 29 doctor

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
我會(huì)做
df = pd.Series(x).str.split(':',expand=True)
df.columns = ['Name','Age', 'Occupation']
df
Out[172]:
Name Age Occupation
0 john 42 engineer
1 michael 29 doctor

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
不確定這是最好的方法,但是......
x = ['john:42:engineer', 'michael:29:doctor']
x = [i.split(':') for i in x]
pd.DataFrame({'name': [i[0] for i in x], 'age': [i[2] for i in x], 'occupation': [i[1] for i in x]})
Output:
name age occupation
0 john 42 engineer
1 michael 29 doctor
添加回答
舉報(bào)