按模式分割列很容易:
import pandas as pd
_df = pd.DataFrame([['1 / 2 / 3', '4 / 5 / 6'], ['7 / 8 / 9', '10 / 11 / 12']])
_df.apply(lambda x: x.str.split(' / '))
0 1
0 [1, 2, 3] [4, 5, 6]
1 [7, 8, 9] [10, 11, 12]
但是如何使用expand=True
作为多索引来创建数据框?我不知道可以在哪里传递索引。
_df.apply(lambda x: x.str.split(' / ', expand=True))
ValueError: If using all scalar values, you must pass an index
预期的输出(列名并不重要,可以是任意的::
A B
a b c a b c
0 1 2 3 4 5 6
1 7 8 9 10 11 12