连接熊猫中的一列和多行

时间:2019-11-08 00:51:02

标签: python pandas

我不太了解Python的Pandas库。 也许你们中的一个可以帮助我。

我有一个列表,代表一个表的第一列,第二个列表,代表行。 (第一个列表中填有工作时间表的名称,第二个列表中包含当天的班次)。

我的意图是将他们聚在一起,以便看起来像这样:

 information = null;
...
  ngOnInit() {
     // Get the ID that was passed with the URL
     let id = this.activatedRoute.snapshot.paramMap.get('id');

     // Get the information from the API
     this.userService.getUserDetails(id).subscribe(result => {
       this.information = result;
       console.log(result);
     });
   }

列表如下:

      | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
Peter |  X1 | x1  |  -  |  -  | X2  | X2  |  -  |
Jeff  |  X2 | x2  |  -  |  -  | X1  | X1  |  -  |
Max   |  -  |  -  |  X1 |  X1 |  -  |  -  |  X2 |

我希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

按照Pandas文档,创建一个DataFrame(请参阅文档here),当您有一个列标题列表,然后每行还有其他列表时,您可以执行以下操作:

headers = ["A", "B", "C"]
indexes = ["Peter", "Pepito", "Maria"]
rows = [
  [1, 2, 3], 
  [1, 2, 3],
  [1, 2, 2]
]

df = pd.DataFrame(data=rows, columns=headers, index=indexes)
相关问题