组合 – Excel电子表格中的两套
很难解释这一点,但我会坚持下去。
我正在使用NodeXL来查看数据集中的关系。 基本上我有一些主题,然后是这些主题的子主题,我需要每个主题的子主题的所有可能的组合列表,但每个组合最多只有两个variables(将有重复的组合,因为有多个主题,我想看看哪些组合是最常见的)。
这是一个例子
input:
Topic Subtopic G/xxx1 Banana G/xxx1 Apple G/xxx1 Pear G/xxx1 Grape G/xxx2 Banana G/xxx2 Grape G/xxx2 Pear
产量
AB Banana Apple Banana Pear Banana Grape Apple Pear Apple Grape Pear Grape Banana Grape Banana Pear Grape Pear
我希望这是有道理的,如果有人有这样做的好方法,我会非常感激。 我不能手动做,因为我有成千上万的话题。
我用csv格式做了这个,但你可以调整它。 我怎么做(使用pandas阅读数据框):
import pandas as pd df = pd.read_csv("data.csv") combinations = [] for index, subset in df.groupby("Topic"): subtopics = list(subset["Subtopic"]) n = len(subtopics) for i in range(n): for j in range(i): combinations.append(2(subtopics[i],subtopics[j])) print combinations print len(combinations)
无论如何,最好的方法来创build所有的对。 我正在研究可能的第二个解决scheme。 将尽快发布;)
编辑:
这是我的第二个解决scheme(仍然使用pandas):
将pandas导入为pd
df = pd.read_csv("data.csv") indexedDF = df.reset_index().set_index("Topic") mergedDF = pd.merge(indexedDF,indexedDF, left_index=True, right_index=True) finalDF = mergedDF[mergedDF["index_x"] > mergedDF["index_y"]] print finalDF