Construct pandas DataFrame from list of tuples of (row,col,values)(从 (row,col,values) 的元组列表构造 pandas DataFrame)
问题描述
我有一个像这样的元组列表
I have a list of tuples like
data = [
('r1', 'c1', avg11, stdev11),
('r1', 'c2', avg12, stdev12),
('r2', 'c1', avg21, stdev21),
('r2', 'c2', avg22, stdev22)
]
我想将它们放入一个 pandas DataFrame 中,其中行由第一列命名,列由第二列命名.处理行名的方法似乎类似于 pandas.DataFrame([x[1:] for x in data], index = [x[0] for x in data])但是如何处理列以获得 2x2 矩阵(前一组的输出是 3x4)?有没有更智能的方法来处理行标签,而不是明确地忽略它们?
and I would like to put them into a pandas DataFrame with rows named by the first column and columns named by the 2nd column. It seems the way to take care of the row names is something like pandas.DataFrame([x[1:] for x in data], index = [x[0] for x in data]) but how do I take care of the columns to get a 2x2 matrix (the output from the previous set is 3x4)? Is there a more intelligent way of taking care of row labels as well, instead of explicitly omitting them?
编辑 看来我需要 2 个数据帧 - 一个用于平均值,一个用于标准偏差,对吗?或者我可以在每个单元格"中存储一个值列表吗?
EDIT It seems I will need 2 DataFrames - one for averages and one for standard deviations, is that correct? Or can I store a list of values in each "cell"?
推荐答案
你可以在创建后旋转你的DataFrame:
You can pivot your DataFrame after creating:
>>> df = pd.DataFrame(data)
>>> df.pivot(index=0, columns=1, values=2)
# avg DataFrame
1 c1 c2
0
r1 avg11 avg12
r2 avg21 avg22
>>> df.pivot(index=0, columns=1, values=3)
# stdev DataFrame
1 c1 c2
0
r1 stdev11 stdev12
r2 stdev21 stdev22
这篇关于从 (row,col,values) 的元组列表构造 pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 (row,col,values) 的元组列表构造 pandas DataFrame
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
