简体   繁体   中英

How to select rows with more than one value in Pandas DataFrame

I have a DataFrame that looks like this:

    Entry   ribosome protein    PDB
0   P46782            s5         4ug0;4v6x;5a2q;5aj0;5flx;5lks;5oa3;5t2c;5vyc;6...
1   P0A7W3            s5         5wf0;5wfs;6awb;6awc;6awd
2   A2RNN6            s5         5myj
3   Q5SHQ5            s5         1fjg;1fka;1hnw;1hnx;1hnz;1hr0;1i94;1i95;1i96;1...
4   Q2YYL4            s5         6fxc
5   A0QSG6            s5         5o5j;5o61;5xyu;5zeb;5zep;5zeu;6dzi;6dzk
6   P33759            s5         5mrc;5mre;5mrf`

I need to extract rows that have more than one entry in a column 'PDB'. For example, I want to have the DataFrame that shows rows without "6fxc" and "5myj" (single entries) in this case, but only multiple PDBs like "5mrc;5mre;5mrf".

How to do it?

This is only a fragment of a huge dataframe with such data, that I need to filter this way.

May be you can use something with split and len and followed by filtering it:

df[df['PDB'].str.split(';').str.len()>1]

Following comment, you can also try simply counting ; as following:

df[df['PDB'].str.count(";")>0]

You can omit the rows whose PDB field contains no ; like this:

df[df['PDB'].str.contains(';')]

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