<p>这就是我在griggy回答的评论中提到的代码</p>
<pre><code>import pandas as pd
import itertools
import collections
# Creating the dataframe
df = pd.DataFrame({"QUESTIONCODE": [1,1,2,3,4,4],
"SUBJETCS": ["English", "French", "English", "Japanese", "English", "Japanese"]})
# Pivoting to get the counts of each subject
dfPivot = pd.pivot_table(df, values='QUESTIONCODE', index='SUBJETCS', columns='SUBJETCS', aggfunc='count', fill_value=0)
# Creating a dataframe with each QUESTIONCODE and its SUBJECTs
x = df.groupby("QUESTIONCODE")["SUBJETCS"].apply(",".join).reset_index()
# Mapping QUESTIONCODE to its SUBJECTs as a dictionary
dictCombos = dict(zip(x["QUESTIONCODE"].tolist(), [s.split(",") for s in x["SUBJETCS"].tolist()]))
# Creating a list with all possible pair of SUBJECTs
list_all_pairs = []
for k, v in dictCombos.items():
prm = list(itertools.permutations(v, 2))
list_all_pairs.extend(prm)
# Creating a dictionary of all pairs of Materias and its counts
dictCounts = dict(collections.Counter(list_all_pairs))
# Filling the dfPivot dataframe with all pairs of Materias and its counts
for pairs, counts in dictCounts.items():
dfPivot.loc[pairs] = counts
</code></pre>