现在有很多类似的问题,但大多数都回答了如何删除重复的列。但是,我想知道如何制作一个元组列表,其中每个元组都包含重复列的列名。我假设每个列都有一个唯一的名称。为了进一步说明我的问题:
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],'B': [2, 4, 2, 1, 9],
'C': [1, 2, 3, 4, 5],'D': [2, 4, 2, 1, 9],
'E': [3, 4, 2, 1, 2],'F': [1, 1, 1, 1, 1]},
index = ['a1', 'a2', 'a3', 'a4', 'a5'])
然后我要输出:
^{pr2}$如果你今天感觉很好,那么也可以把同样的问题扩展到行。如何获取每个元组包含重复行的元组列表。在
这也应该做到:
产量:
^{pr2}$这里有一个新方法-
样本运行-
^{pr2}$转换为执行相同的操作,但是对于行(索引),我们只需要沿着另一个轴切换操作,如下-
样本运行-
标杆管理
方法-
注意:
@John Galt's soln-2
不包括在内,因为大小为(8000,500)
的输入将被提议的broadcasting
爆炸。在时间安排-
使用NumPy的查看功能进行超级增强
利用NumPy的视图功能,我们可以将每个元素组看作一个数据类型,我们可以获得更显著的性能提升,比如-
时间安排-
只是疯狂的加速!在
这是一个单一的班轮
或者,使用NumPy广播。更好的,看看Divakar的solution
^{pr2}$相关问题 更多 >
编程相关推荐