通过逗号python将数据框分成两列

时间:2017-08-09 20:32:57

标签: python python-2.7 python-3.x

需要一些帮助 - 我认为这不是太困难,但有心理障碍。 我试图将数据帧与一列转换为两列,并用逗号分隔。以下是我正在使用的代码:

log_model2coeff_df = log_model2coeff_df['lol'].apply(lambda x :pd.Series(x.split(',')))


+-----------------------------------------------+
|                      lol                      |
+-----------------------------------------------+
| ('Principal', array([-0.00088419]))           |
| ('age', array([ 0.01724407]))                 |
| ('Male', array([-0.31822202]))                |
| ('High School or Below', array([-0.0003084])) |
| ('Master or Above', array([ 0.02205005]))     |
| ('college', array([ 0.10759677]))             |
+-----------------------------------------------+

我希望它看起来像:

+---------------------------------------------------+
|        lol                |         lol2          |                                                           
+---------------------------------------------------+
| ('college'              |  array([ 0.10759677]))  |
| ('age'                  |  array([ 0.01724407]))  |
| ('Principal'            |  array([-0.00088419]))  |
| ('Master or Above'      |  array([ 0.02205005]))  |
| ('Male'                 |  array([-0.31822202]))  |
| ('High School or Below' |  array([-0.0003084]))   |
+----------------------------------------------------


Thanks in advance

1 个答案:

答案 0 :(得分:0)

最简单的方法是将分割结果存储在中间变量中。

split = df['lol'].str.split(',')
df['lol'] = split.apply(lambda x: x[0])
df['lol2'] = split.apply(lambda x: x[1])