Home > Net >  Sort df columns consisting of list of string and zeros alphabetically
Sort df columns consisting of list of string and zeros alphabetically

Time:04-03

I have a pandas df with several columns consisting of string and zeros. I would like it to be listed alphabetically with the zeros at the end. I found ways to sort it with a list of strings, but I have strings and numbers it is part of a df.

Edit, one col of df is float(C in this case) and should not be part of function

import pandas as pd

dfA = {'A':['A,C,F,E,B', 'E,B,A,A,C', '0,B,F,E,F', 'F,0,E,C,C', 'C,A,A,E,0'],
        'B': [ 'B,E,A,0,A', '0,B,A,E,0', 'B,A,F,0,B', 'C,B,A,F,D', '0,0,C,D,A'],
'C': [1,4,12,7,9] } 
dfA = pd.DataFrame(dfA)
print(dfA)
           A          B  C         
0  A,C,F,E,B  B,E,A,0,A  1
1  E,B,A,A,C  0,B,A,E,0  4
2  0,B,F,E,F  B,A,F,0,B  12
3  F,0,E,C,C  C,B,A,F,D  7
4  C,A,A,E,0  0,0,C,D,A  9

Wished output

          A          B   C     
0 A,B,C,E,F  A,A,B,E,0   1
1 A,A,B,C,E  A,B,E,0,0   4
2 B,E,F,F,0  A,B,B,F,0   12
3 C,C,E,F,0  A,B,C,D,F   7
4 A,A,C,E,0  A,C,D,0,0   9

CodePudding user response:

I did the following with the code of Ynjxsjmh

f=(lambda x: ','.join(sorted(x.split(','), key=lambda item: (1 if item.isdigit() else 0, item))))

df = df.apply(lambda x: f(x.A), axis=1)

And get the wished result for one column as wanted and coded. How can I apply to whole df except one col?

CodePudding user response:

First you can use pandas.DataFrame.applymap to apply function to a Dataframe elementwise.

Then you could provide a custom key argument to sorted method which gives a lower value to strings than it does to ints to sort digit after string.

dfA[['A', 'B']] = dfA[['A', 'B']].applymap(lambda x: ','.join(sorted(x.split(','), key=lambda item: (1 if item.isdigit() else 0, item))))
print(df_)

           A          B
0  A,B,C,E,F  A,A,B,E,0
1  A,A,B,C,E  A,B,E,0,0
2  B,E,F,F,0  A,B,B,F,0
3  C,C,E,F,0  A,B,C,D,F
4  A,A,C,E,0  A,C,D,0,0
  • Related