pandas 按列表过滤熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45040312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:58:30  来源:igfitidea点击:

Filter pandas dataframe by list

pythonpandasnumpydata-science

提问by julianstanley

I have a dataframe that has a row called "Hybridization REF". I would like to filter so that I only get the data for the items that have the same label as one of the items in my list.

我有一个数据框,其中有一行名为“Hybridization REF”。我想进行过滤,以便只获取与我的列表中的项目之一具有相同标签的项目的数据。

Basically, I'd like to do the following:

基本上,我想做以下事情:

dataframe[dataframe["Hybridization REF'].apply(lambda: x in list)] 

but that syntax is not correct.

但该语法不正确。

回答by Pranav Gandhi

Suppose dfis your dataframe, lstis our listof labels.

假设 df是你的dataframelst是我们list的标签。

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.

将显示其索引与列表项的任何值匹配的所有行。我希望这有助于解决您的查询。

回答by Scott Boston

Update using reindex,

使用重新索引更新,

df.reindex(collist, axis=1)

and

df.reindex(rowlist, axis=0)

and both:

和两者:

df.reindex(index=rowlist, columns=collist)


You can use .loc or column filtering:

您可以使用 .loc 或列过滤:

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

回答by Sandeep

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

是否有一个 numpy 数据框?我猜它是Pandas数据框,如果是这样,这是解决方案。

df[df['Hybridization REF'].isin(list)]