如何基于字符串值列表对 pandas 数据框进行子集设置? [英] How do I subset a pandas data frame based on a list of string values?

查看:96
本文介绍了如何基于字符串值列表对 pandas 数据框进行子集设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dF,它的长度超过10万行,宽度为几列,这没什么疯狂的.我试图基于约4000个字符串的列表来对行进行子集化,但仍在努力寻找方法.有没有一种方法可以使用类似的子集.

I've got a dF that's over 100k rows long, and a few columns wide — nothing crazy. I'm trying to subset the rows based on a list of some 4000 strings, but am struggling to figure out how to do so. Is there a way to subset using something like.

dF看起来像这样

dog_name    count
===================
Jenny        2
Fido         4
Joey         7
Yeller       2

字符串列表包含在变量dog_name_list=['Fido', 'Yeller']

and the list of strings is contained the variable dog_name_list=['Fido', 'Yeller']

我已经尝试了以下方法 df[df['dog_name'].isin(dog_name_list),但遇到一个有趣的错误:unhashable type: 'list'

I've tried something along the lines of df[df['dog_name'].isin(dog_name_list), but am getting a fun error: unhashable type: 'list'

我已经检查了类似的问题文档

I've checked a similar question, the docs and this rundown for subsetting data frames by seeing whether a value is present in a list, but that's got me right about nowhere, and I'm a little confused by what I'm missing. Would really appreciate someone's advice!

推荐答案

我相信您的狗名"列中有一个列表.

I believe you have a list in your dog name column.

这很好:

>>> df[df['dog_name'].isin(['Fido', 'Yeller'])]
  dog_name  count
1     Fido      4
3   Yeller      2

但是,如果您添加列表:

But if you add a list:

df.ix[4] = (['a'], 2)
>>> df
  dog_name  count
0    Jenny      2
1     Fido      4
2     Joey      7
3   Yeller      2
4      [a]      2

>>> df[df['dog_name'].isin(['Fido', 'Yeller'])]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-1b68dd948f39> in <module>()
----> 1 df[df['dog_name'].isin(['Fido', 'Yeller'])]
...
pandas/lib.pyx in pandas.lib.ismember (pandas/lib.c:5014)()

TypeError: unhashable type: 'list'

要找到那些坏狗:

>>> df[[isinstance(dog, list) for dog in df.dog_name]]
  dog_name  count
4      [a]      2

要查找列中的所有数据类型:

To find all the data types in the column:

>>> set((type(dog) for dog in df.dog_name))
{list, str}

这篇关于如何基于字符串值列表对 pandas 数据框进行子集设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆