Using Pandas I have a df that is 14000 rows by 56 columns (keywords) I have a keyword list (full_keys) that is 1406 items and an empty (0) dataframe (called key_frame)that uses the keyword list as index and columns (so 1406x1406)
I want to iterate through each row of the df, and for each keyword in the row, increment the intersection in key_frame of that keyword and each other keyword that exists in the row.
key_frame = pd.DataFrame(columns=full_keys, index=full_keys).fillna(0) # create empty df
for i, r in keywords.iterrows(): #iterate through each row in df
for index, rows in key_frame.iterrows(): #iterate through blank df to tabulate
if index in list(r): # if the index (which is a keyword) appears in row...
for x in r: #iterate through row and for each intersection loc[index, x] increment tally by 1
key_frame.loc[index, x]+=1
I also have this, same as above but for commented line:
key_frame_2 = pd.DataFrame(columns=full_keys, index=full_keys).fillna(0)
for i, r in keywords.iterrows():
for x in full_keys: #iterate through list rather than key_frame, it's...faster?
if x in list(r):
for keyword in r:
key_frame_2.loc[x, keyword]+=1
Feeling rusty and would love some pointers! I know there is a better way to do this.