Filter pandas dataframe by list

£可爱£侵袭症+ 提交于 2019-11-28 14:50:14

You can use .loc or column filtering:

df = pd.DataFrame(data=np.random.rand(5,5),columns=list('ABCDE'),index=list('abcde'))

df
          A         B         C         D         E
a  0.460537  0.174788  0.167554  0.298469  0.630961
b  0.728094  0.275326  0.405864  0.302588  0.624046
c  0.953253  0.682038  0.802147  0.105888  0.089966
d  0.122748  0.954955  0.766184  0.410876  0.527166
e  0.227185  0.449025  0.703912  0.617826  0.037297

collist = ['B','D','E']

rowlist = ['a','c']

Get columns in list:

df[collist]

Output:

          B         D         E
a  0.174788  0.298469  0.630961
b  0.275326  0.302588  0.624046
c  0.682038  0.105888  0.089966
d  0.954955  0.410876  0.527166
e  0.449025  0.617826  0.037297

Get rows in list

df.loc[rowlist]

          A         B         C         D         E
a  0.460537  0.174788  0.167554  0.298469  0.630961
c  0.953253  0.682038  0.802147  0.105888  0.089966
Pranav Gandhi

Suppose df is your dataframe, lst is our list of labels.

df.loc[ df.index.isin(lst), : ]

Will display all rows whose index matches any value of the list item. I hope this helps solve your query.

Is there a numpy dataframe? I am guessing it is pandas dataframe, if so here is the solution.

df[df['Hybridization REF'].isin(list)]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!