I have the below pandas data frame and I am trying to find the column names in descending order for each row.
A B C D
A 1 3 2 1
B 2 1 5 0
C 1 0 1 9
D 2 0 1 2
For each row I am trying to get the column indexes in sorted order ( descending )
I should get :
B C D A
C A B D
and so on
Is this possible using pandas.?
I am trying something like this.
test[2].sort_index(axis = 0, ascending = True)
>>> df1
user_handle 1 2 3 4 5 6 8 9 10 12
user_handle
1 6 0 0 1 0 0 0 0 0 0
2 0 95 0 0 1 0 0 0 1 0
3 0 0 2 0 0 0 0 0 0 0
4 1 0 0 12 0 1 0 0 2 0
5 0 1 0 0 9 0 0 0 0 0
6 0 0 0 1 0 14 0 0 1 0
8 0 0 0 0 0 0 4 0 0 0
9 0 0 0 0 0 0 0 12 0 0
10 0 1 0 2 0 1 0 0 49 0
12 0 0 0 0 0 0 0 0 0 2
>>> t1 = np.flip(df1.values.argsort(), 1)
>>> pd.DataFrame(df1.columns[t1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "python3.6/site-packages/pandas/core/frame.py", line 303, in __init__
dtype=dtype)
File "python3.6/site-packages/pandas/core/frame.py", line 411, in _init_dict
return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "python3.6/site-packages/pandas/core/frame.py", line 5506, in _arrays_to_mgr
return create_block_manager_from_arrays(arrays, arr_names, axes)
File "python3.6/site-packages/pandas/core/internals.py", line 4310, in create_block_manager_from_arrays
mgr = BlockManager(blocks, axes)
File "python3.6/site-packages/pandas/core/internals.py", line 2792, in __init__
(block.ndim, self.ndim))
AssertionError: Number of Block dimensions (3) must equal number of axes (2)
>>>
KeyboardInterrupt
You can use :
temp = np.flip(df.values.argsort(),1)
ndf = pd.DataFrame(df.columns[temp])
0 1 2 3
0 B C D A
1 C A B D
2 D C A B
3 D A C B
By using rank
and unstack
df.stack().groupby(level=0).rank(method ='first',ascending =False).reset_index(level=1).set_index(0,append=True).unstack()
Out[309]:
level_1
0 1.0 2.0 3.0 4.0
A B C A D
B C A B D
C D A C B
D A D C B
And using numpy
pd.DataFrame(df.columns.values[np.lexsort(([df.columns.values]*len(df),df.values))]).iloc[:, ::-1]
Out[351]:
3 2 1 0
0 B C D A
1 C A B D
2 D C A B
3 D A C B
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With