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").
source
share