python - First name of Pandas column with sorted list -


suppose have following dataframe:

df = pd.dataframe({'ab': [['ab', 'ef', 'bd'], ['abc', 'efg', 'cd'], ['bd', 'aaaa']],                    'cd': [['xy', 'gh'], ['trs', 'abc'], ['ab', 'bcd', 'efg']],                      'ef': [['uxyz', 'abc'], ['peter', 'adam'], ['ab', 'zz', 'bd']]})  df                 ab              cd             ef 0    [ab, ef, bd]        [xy, gh]    [uxyz, abc] 1  [abc, efg, cd]      [trs, abc]  [peter, adam] 2      [bd, aaaa]  [ab, bcd, efg]   [ab, zz, bd] 

i want extract column contains sorted list. in case cd, since ['ab','bcd','efg'] sorted in ascending order. guaranteed no list empty , contain @ least 2 elements. stuck @ how combine applymap , sort function using pandas ? tried come solution here couldn't figure out way combine applymap , sort.

i working in python 2.7 , pandas

use applymap sorted

in [2078]: df.applymap(sorted).eq(df).any() out[2078]: ab    false cd     true ef    false dtype: bool 

get result list

in [2081]: cond = df.applymap(sorted).eq(df).any()  in [2082]: cond[cond].index out[2082]: index([u'cd'], dtype='object')  in [2083]: cond[cond].index.tolist() out[2083]: ['cd'] 

if need specific columns data

in [2086]: df.loc[:, cond] out[2086]:                cd 0        [xy, gh] 1      [trs, abc] 2  [ab, bcd, efg] 

and, first of column name

in [2092]: cond[cond].index[0] out[2092]: 'cd' 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -