gungor
gungor

Reputation: 417

How to select from different columns conditionally in pandas

I have an pandas DataFrame like shaped Nx5

['','','A','','']
['','C','','','']
['','A','','','']
['','','','T','']
.
.
.

I want to convert it to Nx1 shape getting non-empty values

['A']
['C']
['A']
['T']
.
.
.

How can that be done?

Upvotes: 1

Views: 212

Answers (1)

HYRY
HYRY

Reputation: 97331

You can call "".join for every row:

df.apply("".join, axis=1)

If you are not sure every row has only one not empty value, following method is better:

import pandas as pd

df = pd.DataFrame(
    [['','','A','',''],
    ['','C','','',''],
    ['','A','','',''],
    ['','','','T','']]
)

s = df.stack()
print s[s!=""]

output:

0  2    A
1  1    C
2  1    A
3  3    T
dtype: object

for more than one column:

r = s[s!=""]
r.groupby(level=0).apply(pd.DataFrame.reset_index, drop=True).unstack()

Upvotes: 2

Related Questions