Unable to sort columns in Pandas
The following should sort the names of my data frame columns based on
their mean value:
sorted(a.columns, key=lambda x: mydf.mean().loc[x])
so that I could use it to sort the actual columns as:
keyf = key=lambda x: mydf.mean()[x]), axis=1
mydf = mydf.reindex_axis(sorted(mydf.columns, key = keyf)
But instead I get
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
Why?
Here is the data frame mydf:
Something Foo Bar Positive Negative
0 0.690489 0.475099 0.613022 0.690489 0.481065
1 0.633300 0.489058 0.620519 0.633300 0.504669
2 0.578095 0.457907 0.488271 0.578095 0.493823
3 0.680185 0.373466 0.579683 0.680185 0.564603
4 0.546329 0.527526 0.497956 0.546329 0.490985
5 0.539669 0.388896 0.553585 0.539669 0.502550
6 0.616210 0.490364 0.411997 0.616210 0.280764
7 0.463462 0.491058 0.577513 0.463462 0.491796
8 0.571062 0.506782 0.418724 0.571062 0.355602
9 0.642424 0.435661 0.514894 0.642424 0.485734
10 0.562427 0.518879 0.510625 0.562427 0.330446
11 0.651997 0.495033 0.485939 0.651997 0.541905
12 0.651075 0.586200 0.556045 0.651075 0.455618
13 0.642308 0.475351 0.454831 0.642308 0.494306
14 0.631409 0.387464 0.447110 0.631409 0.411720
15 0.610304 0.455503 0.569653 0.610304 0.319964
16 0.554888 0.482659 0.572695 0.554888 0.528743
17 0.620681 0.558925 0.585417 0.620681 0.463687
18 0.453624 0.407804 0.461207 0.453624 0.524084
19 0.623790 0.519005 0.451432 0.623790 0.449669
No comments:
Post a Comment