I have the following DateFrame:
| tag | list |
| -------- | ----------------------------------------------------|
| icecream | [['A',0.9],['B',0.6],['C',0.5],['D',0.3],['E',0.1]] |
| potato | [['U',0.8],['V',0.7],['W',0.4],['X',0.3],['Y',0.2]] |
The column list is a list of lists with each list having an item and a value between 1 to 0. The lists are arranged in descending order of this value.
I want to extract each item from here and get the top 3 item but not the item itself. Resultant data frame should be:
| item | top_3 |
| ---- | --------------------------------|
| A | [['B',0.6],['C',0.5],['D',0.3]] |
| B | [['A',0.9],['C',0.5],['D',0.3]] |
| C | [['A',0.9],['B',0.6],['D',0.3]] |
| D | [['A',0.9],['B',0.6],['C',0.5]] |
| E | [['A',0.9],['B',0.6],['C',0.5]] |
| U | [['V',0.7],['W',0.4],['X',0.3]] |
| V | [['U',0.8],['W',0.4],['X',0.3]] |
| W | [['U',0.8],['V',0.7],['X',0.3]] |
| X | [['U',0.8],['V',0.7],['W',0.4]] |
| Y | [['U',0.8],['V',0.7],['W',0.4]] |
I tried and I am able to extract the value, I am stuck at the part where I want to ignore the item itself while creating the top_3. This is what I have done:
data = [['icecream', [['A', 0.9],['B', 0.6],['C',0.5],['D',0.3],['E',0.1]]],
['potato', [['U', 0.8],['V', 0.7],['W',0.4],['X',0.3],['Y',0.2]]]]
df = pd.DataFrame(data, columns=['tag', 'list'])
df
--
temp = {}
for idx, row in df.iterrows():
for item in row["list"]:
temp[item[0]] = row["tag"]
top_items = {}
for idx, row in df.iterrows():
top_items[row["tag"]] = row["list"]
similar = []
for item, category in temp.items():
top_3 = top_items.get(category)
sample = top_3[:3]
similar.append([item, sample])
df = pd.DataFrame(similar)
df.columns = ["item", "top_3"]
My result:
| item | top_3 |
| ---- | --------------------------------|
| A | [['A',0.9],['B',0.6],['C',0.5]] |
| B | [['A',0.9],['B',0.6],['C',0.5]] |
| C | [['A',0.9],['B',0.6],['C',0.5]] |
| D | [['A',0.9],['B',0.6],['C',0.5]] |
| E | [['A',0.9],['B',0.6],['C',0.5]] |
| U | [['U',0.8],['V',0.7],['W',0.4]] |
| V | [['U',0.8],['V',0.7],['W',0.4]] |
| W | [['U',0.8],['V',0.7],['W',0.4]] |
| X | [['U',0.8],['V',0.7],['W',0.4]] |
| Y | [['U',0.8],['V',0.7],['W',0.4]] |
You see, the top_3 is wrong for A,B,C,U,V,W because in all cases it takes top 3 and thus doesn't care about the item itself.
The result I get is always bringing the top 3 and I tried to put filters but unable to get it working.
If there are better ways to extract the data than how I did, do let me know ways to optimize it.
