简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM