将多个列表转换为一个DataFrame

时间:2019-01-29 10:32:12

标签: pandas list tuples transform

我正在寻找一种方便,简单的方法,将三个列表的元组组合到一个总共六列的数据帧(?)中。

<?php
  $teamString = "Benfica (U23):Vitoria Guimaraes U23";
  $masterArray = ['Benfica','Guimares','U23','Vitoria'];
  $pattern = '/[^a-zA-Z\d]/';
  $teamArray = preg_split( $pattern, $teamString );
  sort($teamArray);
  $interArray = array_unique($teamArray); //to remove duplicate U23
  $finalArray = array_filter($interArray, function($k) {
    return (preg_replace('/\s+/', '', $k) != ''); //to get rid of whitespace
  });
  //...compare $finalArray with $masterArray

对于列表的两列中的每列,我想使用“ Word”和“ Value”作为标题。

我想要的输出:

list1 = [('word1', 0.6037212610244751), ('word2', 0.5418980717658997),('word3', 0.5389199256896973), ('word4', 0.51054847240448), ('word5', 0.49834704399108887)]

list2 = [('word6', 0.6037212610244751), ('word7', 0.5418980717658997),('word8', 0.5389199256896973), ('word9', 0.51054847240448), ('word10', 0.49834704399108887)]

list3 =[('word11', 0.6037212610244751), ('word12', 0.5418980717658997),('word13', 0.5389199256896973), ('word14', 0.51054847240448), ('word15', 0.49834704399108887)]

到目前为止,我已经尝试过:

Word   Score   Word   Score   Word   Score
word1  0.603   word6  0.603   word11 0.603
...
word5  0.498   ...

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

IIUC在axis=1上使用pd.concat()

pd.concat([pd.DataFrame(list1,columns=['word','values']),pd.DataFrame(list2,columns=['word','values']),pd.DataFrame(list3,columns=['word','values'])],axis=1)

    word    values    word    values    word    values
0  word1  0.603721   word6  0.603721  word11  0.603721
1  word2  0.541898   word7  0.541898  word12  0.541898
2  word3  0.538920   word8  0.538920  word13  0.538920
3  word4  0.510548   word9  0.510548  word14  0.510548
4  word5  0.498347  word10  0.498347  word15  0.498347