【问题标题】:How to get data set using python or C++如何使用 python 或 C++ 获取数据集
【发布时间】:2020-11-02 11:22:17
【问题描述】:

我的数据如下:

如果我给每一行命名:a1 a2 b1 b2 c1 c2 d1 d2。规则是:A B C D,你可以在每一大行交换位置。我需要一组 4 个数字,所以我将拥有:

  • a1 b1 c1 d1
  • a2 b1 c1 d1
  • a1 b2 c2 d2
  • a2 b2 c2 d2
  • a1 b1 c2 d2
  • a2 b1 c2 d2
  • a1 b1 c1 d2
  • a2 b1 c1 d2
  • a1 b2 c1 d2
  • a2 b2 c1 d2
  • a1 b1 c1 d2
  • a2 b1 c1 d2
  • a1 b1 c2 d1
  • a2 b1 c2 d1
  • a1 b2 c2 d1
  • a2 b2 c2 d1

因此,当我更改数字时,我将拥有许多数据集。 如何过滤以获取数据集的唯一性。并计算每个唯一集合出现的次数。

【问题讨论】:

    标签: python c++ excel


    【解决方案1】:

    嗯,基因,很好吃……

    所以,要在 Python 中解决这个问题,你应该这样做:

    1. 从 xmls 中获取数据(看起来像 xmls)。这个,只用熊猫:pd.read_excel()
    2. (可选步骤)准备数据。我看到一个单元格没有值,这可能会导致一些问题。
    3. 根据需要创建索引(a1、a2 等)。您可以使用 for-loor 生成它,并将列表作为返回,然后使用 pd.set_index()
    4. 主要思想:创建 2 个 for 循环:一个用于,比如说,静态组件(外循环),另一个用于动态组件(内循环)。
    5. 在您的示例中:
      • a1 b1 c1 d1
      • a1 b1 c1 d1
      • a2 b1 c1 d1

    静态是“b1 c1 d1”,动态是“a1”-->“a2”。

    一次迭代后,静态组件必须更改“b1 c1 d1”->“b2 c2 d2”。 所有迭代都必须以将集合添加到您创建的列表 (list.append(set)) 中结束。

    经过上面的操作,你需要过滤这个。步骤是:

    1. 创建一个空字典,其中键是唯一元素的表示,值是它出现的次数
    2. 使 for 循环类似于:
      for set in list_of_sets: if set not in dict: dict[set] = 1 else: dict[set] += 1

    或者您可以使用collection.Counternp.unique()(EXAMPLE)。
    我希望它能帮助您完成任务。

    【讨论】:

      【解决方案2】:

      谢谢 Roman_N,这是我的代码:

      import pandas as pd
      import xlrd
      import functools, operator
      import itertools
      from collections import Counter
      
      df = pd.read_csv("BN.csv")
      
      result = []
      for index,row in df.iterrows():
          s = [[row['a1'],row['a2']], [row['b1'],row['b2']], [row['c1'],row['c2']], [row['d1'],row['d2']]]
          for item in list(itertools.product(*s)):
              result.append(item)
      
      # print(result)
      
      counts = Counter(item for item in result)
      
      for element in counts:
          print(element, counts[element])
      
      print(list, 'length is', len(counts))
      

      【讨论】:

        猜你喜欢
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 2022-12-16
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        相关资源
        最近更新 更多