Reputation: 13
I worked this on my Jupyter.
I'd like to know if there's a way to find the location (column index) of the max value in everyrow in the table.
For example, it looks like this:
yo1 = [1,3,7]
yo2 = [2,4,5,6,8]
yo3 = [0.1,0.3,0.7]
yo4 = [0.2,0.4,0.5,0.6,0.8]
yoo = []
for x in yo3:
vvv = []
for y in yo4:
dot = x*y
na = x+x
nb = y+y
prod = dot/(na+nb)
vvv.append(prod)
yoo.append(vvv)
yooo = pd.DataFrame(yoo, columns=(yo2), index=[yo1])
print(yooo)
(yes, it is cosine similarity)
output:
2 4 5 6 8
1 0.033333 0.040000 0.041667 0.042857 0.044444
3 0.060000 0.085714 0.093750 0.100000 0.109091
7 0.077778 0.127273 0.145833 0.161538 0.186667
Then, I want to get index of the column with max value in everyrow. I used this :
go = yooo.idxmax().reset_index()
go.columns=['column', 'get']
go
output:
column get
0 2 (7,)
1 4 (7,)
2 5 (7,)
3 6 (7,)
4 8 (7,)
but my desired output is :
output:
column get
0 2 7
1 4 7
2 5 7
3 6 7
4 8 7
I've tried replace the '(' with ' '
go['get']=go['get'].str.replace('(','')
and used lstrip-rstrip
go['get']=go['get'].map(lambda x: x.lstrip('(').rstrip(',)'))
also this one
top_n=1
get = pd.DataFrame({n: yooo[col].nlargest(top_n).index.tolist() for n, col in enumerate(yooo)}).T
They all did'nt work well :(
Help me.. How to solve this and would you explain it to me??? Thankyou!!!
Upvotes: 1
Views: 305
Reputation: 153460
Your real problem is in your dataframe constructor for 'yooo', you are wrapping a list with [] creating a 2d list and thus creating a pd.MultiIndex, hence the tuples (7,). Use this instead:
yooo = pd.DataFrame(yoo, columns=(yo2), index=yo1)
yooo.idxmax()
Output:
2 7
4 7
5 7
6 7
8 7
dtype: int64
And further to get dataframe with column names:
yooo.idxmax().rename_axis('column').rename('get').reset_index()
Output:
column get
0 2 7
1 4 7
2 5 7
3 6 7
4 8 7
Upvotes: 2