简单列表到数据框 [英] Simple list to dataframe

查看:37
本文介绍了简单列表到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个列表:

 [(red, red, 1),
 (red, blue, 3),
 (red, yellow, 2),
 (blue, red, 3),
 (blue, blue, 1),
 (blue, yellow, 4),
 (yellow, red, 2),
 (yellow, blue, 4),
 (yellow, yellow, 1)]

获得像这样的熊猫数据框的最简单方法是什么?

What is the easiest way to get a pandas dataframe like this?

             red    blue   yellow
red           1       3       2                               
blue          3       1       4
yellow        2       4       1

推荐答案

使用DataFrame构造函数并通过

Use DataFrame constructor with pivoting by DataFrame.pivot, last remove index and columns names by DataFrame.rename_axis:

df = pd.DataFrame(L).pivot(0,1,2).rename_axis(index=None, columns=None)
print (df)
        blue  red  yellow
blue       1    3       4
red        3    1       2
yellow     4    2       1

这篇关于简单列表到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆