从列表

时间:2017-10-06 18:46:43

标签: python pandas

我有一个列表的pandas dataframe列,想要从列表字符串中提取数字并将它们添加到各自的列中。

     Column A 
 0      [ FUNNY (1),  CARING (1)]
 1      [ Gives good feedback (17),  Clear communicator (2)]
 2      [ CARING (3),  Gives good feedback (3)]
 3      [ FUNNY (2),  Clear communicator (1)]
 4                                         []
 5                                         []
 6      [ CARING (1),  Clear communicator (1)]

我希望输出看起来如下:

FUNNY     CARING    Gives good feedback    Clear communicator
  1          1            None                   None
  None       None         17                     2
  None       3            3                      None
  2          None         None                   1
  None       None         None                   None

等...

1 个答案:

答案 0 :(得分:1)

$register_data = array( 'username' => 'david', 'password' => 'david', 'first_name' => 'david', 'last_name' => 'david', 'email' => 'david@yahoo.com' ); $query = "SELECT * FROM 'user_info' WHERE 'email' = '".$register_data['email']."' OR 'username' = '".$register_data['username']."'"; 使用apply,然后使用pd.Series并重新塑造extractset_index

unstack

输出:

df['Column A'].apply(pd.Series).stack().str.extract(r'(\w+)\((\d+)', expand=True)\
              .reset_index(1, drop=True).set_index(0, append=True)[1]\
              .unstack(1)

使用新输入数据集编辑:

0 Authentic Caring Classy Funny
0         1      3   None     2
1         2   None      1     2