如何使用daframe中现有列的值创建新列

时间:2016-05-30 13:57:12

标签: python numpy pandas

我有一个如下所示的数据框,

  a       b      c
AAAA    BBB -25488
AAAA    BBB -71364
AAAA    BBB +40632
AAAA    BBB +230015
AAAA    BBB +49284
AAAA    BBB -9239
AAAA    BBB +67887
AAAA    BBB -23350
AAAA    BBB +432591

上面的数据框最后应该是,

   a      b c      d 
AAAA    BBB -  25488
AAAA    BBB -  71364
AAAA    BBB +  40632
AAAA    BBB +  230015
AAAA    BBB +  49284
AAAA    BBB -  9239
AAAA    BBB +  67887
AAAA    BBB -  23350
AAAA    BBB +  432591

任何帮助都会非常感激

1 个答案:

答案 0 :(得分:1)

使用str.extract

print (df)
      a    b        c
0  AAAA  BBB   -25488
1  AAAA  BBB   -71364
2  AAAA  BBB   +40632
3  AAAA  BBB  +230015
4  AAAA  BBB   +49284
5  AAAA  BBB    -9239
6  AAAA  BBB   +67887
7  AAAA  BBB   -23350
8  AAAA  BBB  +432591

df[['c','d']] =  df.c.str.extract('([+-])(\d*)', expand=True)

print (df)
      a    b  c       d
0  AAAA  BBB  -   25488
1  AAAA  BBB  -   71364
2  AAAA  BBB  +   40632
3  AAAA  BBB  +  230015
4  AAAA  BBB  +   49284
5  AAAA  BBB  -    9239
6  AAAA  BBB  +   67887
7  AAAA  BBB  -   23350
8  AAAA  BBB  +  432591