RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1211311
Accepted
Yuriy Tigiev
Yuriy Tigiev
Asked:2021-11-29 15:05:08 +0000 UTC2021-11-29 15:05:08 +0000 UTC 2021-11-29 15:05:08 +0000 UTC

如何生成几个具有自己唯一ID的连续相同组合并将结果保存在df中?

  • 772

如何使用单行表达式生成多个连续相同的combinations具有自己唯一的相同的id并将结果存储在中df?

我们希望简化并提高执行速度的工作代码示例 col5 列包含生成集的唯一标识符gc

import itertools as iter
import pandas as pd 

gc = iter.combinations(range(1, 7), 4)
df = pd.DataFrame(gc, columns=['col1', 'col2', 'col3', 'col4'])

dfr = pd.DataFrame()
for i in range(1,4):
    dfi = df.copy()
    dfi['col5'] = i
    dfr = pd.concat([dfr, dfi])

dfr = dfr.reset_index().drop(columns='index')
print(dfr)

预期结果

      col1  col2  col3  col4  col5
0      1     2     3     4     1
1      1     2     3     5     1
2      1     2     3     6     1
3      1     2     4     5     1
4      1     2     4     6     1
5      1     2     5     6     1
6      1     3     4     5     1
7      1     3     4     6     1
8      1     3     5     6     1
9      1     4     5     6     1
10     2     3     4     5     1
11     2     3     4     6     1
12     2     3     5     6     1
13     2     4     5     6     1
14     3     4     5     6     1
15     1     2     3     4     2
16     1     2     3     5     2
17     1     2     3     6     2
18     1     2     4     5     2
19     1     2     4     6     2
20     1     2     5     6     2
21     1     3     4     5     2
22     1     3     4     6     2
23     1     3     5     6     2
24     1     4     5     6     2
25     2     3     4     5     2
26     2     3     4     6     2
27     2     3     5     6     2
28     2     4     5     6     2
29     3     4     5     6     2
30     1     2     3     4     3
31     1     2     3     5     3
32     1     2     3     6     3
33     1     2     4     5     3
34     1     2     4     6     3
35     1     2     5     6     3
36     1     3     4     5     3
37     1     3     4     6     3
38     1     3     5     6     3
39     1     4     5     6     3
40     2     3     4     5     3
41     2     3     4     6     3
42     2     3     5     6     3
43     2     4     5     6     3
44     3     4     5     6     3
python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    CrazyElf
    2021-11-29T15:38:42Z2021-11-29T15:38:42Z

    好吧,这种方式似乎更快,尽管在您的示例中速度差异仅为 2 倍左右。在较大的表上,也许差异会更显着:

    import itertools as iter
    import pandas as pd 
    
    gc = iter.combinations(range(1, 7), 4)
    df = pd.DataFrame(gc, columns=['col1', 'col2', 'col3', 'col4'])
    
    dfr = pd.concat([df for _ in range(1,4)]).reset_index().drop(columns='index')
    dfr['col5'] = [i for i in range(1,4) for _ in range(df.shape[0])]
    print(dfr)
    

    说明:

    • 组合数据帧最好拿出一个周期。当数据帧在循环内一直增长时,这会大大减慢进程。concat如果您一次完成所有累积的数据帧,速度会快得多。而且由于它们对您来说是相同的(除了最后一列),因此也无需复制它们,您可以将数据框与自身连接所需的次数
    • 最后一列也可以在连接后填写一次,这应该比单独输入每个数据帧然后将它们全部连接起来更快、更容易。

    更新:如果你通过 ,它会更快Numpy,并DataFrame在最后(如果有必要的话)变成。或多或少是这样的:

    import itertools as iter
    import numpy as np
    import pandas as pd 
    
    gc = np.array([list(t) for t in iter.combinations(range(1, 7), 4)])
    col5 = np.array([i for i in range(1,4) for _ in range(gc.shape[0])]).reshape(-1,1)
    dfm = np.hstack((np.vstack(tuple(gc for _ in range(1,4))),col5))
    dfr = pd.DataFrame(dfm, columns=['col1', 'col2', 'col3', 'col4','col5'])
    print(dfr)
    

    在这里,您可以(并且应该)进一步优化,例如,立即创建Numpy所需大小的矩阵,而不是vstackand hstack,而是将矩阵放入已经完成的矩阵中。虽然有必要看看它会如何更快,但没有必要用组合制作许多矩阵副本。

    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5