简体   繁体   中英

How to sort dataframe that contains data in arrays?

I would like to remove these brackets and order the dataframe by the R and SP columns. I've tried several things and I get an error. How can I do this?

                 R                  SP
0     [0.6459896423304917]   [0.619298245614035]       M1
1     [0.6494975520851792]  [0.5859649122807016]       M2
2     [0.6560356111719728]  [0.5999999999999998]       M3
3    [0.45921657645058334]  [0.4649122807017543]       M4
4     [0.6302808610901011]  [0.5263157894736838]       M5
..                     ...                   ...       ...
107   [0.5695814044504143]  [0.4771929824561402]       M6
108   [0.6801428488853647]   [0.582456140350877]       M7

You can try using applymap with a simple lambda function and then sort the dataframe with sort_values :

test = pd.DataFrame(dict(a=[[3],[1],[2]],b=[[4],[5],[6]])) 

test = test.applymap(lambda x: x[0]) # removing lists
# sorting all the columns
test = pd.DataFrame(np.sort(test.values, axis=0), index=test.index, columns=test.columns) 
#test = test.sort_values(by="a") #sorting based on values in a column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM