Xin, ( ) , dictionary_test .
import pandas as pd
dictionary_test = {'A': ['hello', 'byebye', 'howdy'], 'B': ['bonjour', 'hello', 'ciao'], 'C': ['ciao', 'hello', 'byebye']}
df = pd.DataFrame(dictionary_test)
words = {word for col in df.columns for word in df[col]}
d = pd.DataFrame(index = words)
for idx in d.index:
for col in df.columns:
d.loc[idx, col] = 1 if idx in set(df[col]) else 0
:
d
Out[6]:
A B C
hello 1.0 1.0 1.0
byebye 1.0 0.0 1.0
bonjour 0.0 1.0 0.0
howdy 1.0 0.0 0.0
ciao 0.0 1.0 1.0
: ValueError: arrays must all be same length , , :
# find how long the longest list is
longest_list_len = max(map(len, dictionary_test.values()))
dictionary_test = {key: value + [None] * (longest_list_len - len(value)) for key, value in dictionary_test.items()}
dictionary_test. words :
words = {word for col in df.columns for word in df[col] if word != None}
!