Pandas: sort each column individually

My dataframe looks something like this, just a lot bigger.

d = {'Col_1' : pd.Series(['A', 'B']),
 'Col_2' : pd.Series(['B', 'A', 'C']),
 'Col_3' : pd.Series(['B', 'A']),
 'Col_4' : pd.Series(['C', 'A', 'B', 'D']),
 'Col_5' : pd.Series(['A', 'C']),}
df = pd.DataFrame(d)

Col_1  Col_2  Col_3  Col_4  Col_5
  A      B      B      C      A
  B      A      A      A      C
  NaN    C      NaN    B      NaN
  NaN    NaN    NaN    D      NaN

At first I try to sort each column separately. I tried playing with something like: df.sort([lambda x: x in df.columns], axis=1, ascending=True, inplace=True)however I just ended up with errors. How to sort each column separately so that you end up with something like:

Col_1  Col_2  Col_3  Col_4  Col_5
  A      A      A      A      A
  B      B      B      B      C
  NaN    C      NaN    C      NaN
  NaN    NaN    NaN    D      NaN

Secondly, I am looking to combine rows in columns

 df = pd.concat([df,pd.DataFrame(df.sum(axis=0),columns=['Concatenation']).T])

I can combine everything with the line above by replacing np.nan with '', but the result will be split ("AB") together and require an extra step to clear (into something like "A: B").

+4
source share
2 answers

Here is one way:

>>> pandas.concat([df[col].order().reset_index(drop=True) for col in df], axis=1, ignore_index=True)
11:      0    1    2  3    4
0    A    A    A  A    A
1    B    B    B  B    C
2  NaN    C  NaN  C  NaN
3  NaN  NaN  NaN  D  NaN

[4 rows x 5 columns]

, , . DataFrames - . DataFrame , . , , . reset_index. , - , , .

+5

, , .

 pd.DataFrame({key: sorted(value.values(), reverse=True) \
    for key, value in df.to_dict().iteritems()})

 pd.DataFrame({key: sorted(values, reverse=True) \
    for key, values in df.transpose().iterrows()})
0

Source: https://habr.com/ru/post/1544220/


All Articles