Python pandas,错误使用.str.contains搜索dataframe列的子字符串

时间:2018-01-02 21:28:10

标签: python pandas dataframe filtering contains

我很感激您使用python 2.7,pandas 0.22和easygui 0.98.1在此问题上提供的任何帮助。

我试图将csv加载到pandas中,使用easygui从用户选择的列表中指定列名(我认为返回为字符串列表)并在数据帧的某列中搜索子字符串

import easygui as eg
import pandas as pd

# define vars_vars from choices of imagej outputs
vars_vars = eg.multchoicebox
            (msg="\n\n\n\nPlease highlight variables included in ImageJ analysis:",
             title="IF Analysis - 2017",
             choices=["integrated density", "mean", 
                      "mean grey value", "area fraction"])

# add required imagej columns for later processing
vars_vars.insert(0, "label")
vars_vars.insert(0, "#")

#User input for csv file
file = eg.fileopenbox()

# load into dataframe using pandas and assign columns using chosen variables
df = pd.read_csv(file, header=None, names=None)
df.columns = vars_vars

# Search 'label' column for certain substring
df[df['label'].str.contains('substring')]

但是我收到了这个错误:

Traceback (most recent call last):
  File "C:/Users/User/.PyCharmCE2017.3/config/scratches/scratch.py", line 61, in <module>
    df[df['label'].str.contains('nsv')]
  File "C:\Users\User\Miniconda2\envs\test2\lib\site-packages\pandas\core\generic.py", line 3614, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'str'

我试过goolging它,发现了这些修复

df['label'] = df['label'].map(str)
df['label'] = df['label'].astype(str)
df['label'] = df['label'].astype(baseline)

以及我调用整个数据框而不是df [&#39;标签&#39;]的变体。

但是,这些都会导致该修复行传递后没有错误,但是当我执行.str.contains行时,它总是返回一个类似的错误,该行声明dataframe对象没有属性映射(对于.map(str)) )或str(用于.astype(x))行。

print type(df['label'])
print df.dtypes
print df['label'].head()
print (df['label'].info())
print type(df['label'][0])

返回

<class 'pandas.core.frame.DataFrame'>

#                      int64
label                 object
integrated density     int64
dtype: object

                                        label
0         nc4_al1_I+pP_4x_contra_ctx_blue.tif
1        nc4_al1_I+pP_4x_contra_ctx_green.tif
2         nc4_al1_I1+pP_4x_contra_ctx_red.tif
3          nc4_al1_I+pP_4x_contra_hc_blue.tif
4         nc4_al1_I+pP_4x_contra_hc_green.tif

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 695 entries, 0 to 694
Data columns (total 1 columns):
(label,)    695 non-null object
dtypes: object(1)
memory usage: 5.5+ KB
None

Traceback (most recent call last):
  File "C:/Users/Shon/.PyCharmCE2017.3/config/scratches/scratch.py", line 67, in <module>
    print type(df['label'][0])
  File "C:\Users\User\Miniconda2\envs\test2\lib\site-packages\pandas\core\frame.py", line 2137, in __getitem__
    return self._getitem_multilevel(key)
  File "C:\Users\User\Miniconda2\envs\test2\lib\site-packages\pandas\core\frame.py", line 2181, in _getitem_multilevel
    loc = self.columns.get_loc(key)
  File "C:\Users\User\Miniconda2\envs\test2\lib\site-packages\pandas\core\indexes\multi.py", line 2072, in get_loc
    loc = self._get_level_indexer(key, level=0)
  File "C:\Users\User\Miniconda2\envs\test2\lib\site-packages\pandas\core\indexes\multi.py", line 2362, in _get_level_indexer
    loc = level_index.get_loc(key)
  File "C:\Users\User\Miniconda2\envs\test2\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

我非常感谢你们提供的任何帮助。

更新:

原来问题是有多索引数据帧。

df.columns.map(''.join).str.strip()

导致在打印类型时将列更改为一系列,这使我能够正确地.str。包含数据。

1 个答案:

答案 0 :(得分:0)

查看type(df['label']).info()的输出:

<class 'pandas.core.frame.DataFrame'> 
RangeIndex: 695 entries, 0 to 694 
Data columns (total 1 columns): 
(label,) 695 non-null object 
dtypes: object(1) memory usage: 5.5+ KB None

看起来好像这个数据框中的列是一个带有空元素的元组,它表示第二级为空的多索引列标题。

我建议首先尝试修复pd.read_cv,以防止创建多索引。但是,使用:

df.columns = df.columns.map(''.join).str.strip()

将多索引列标题展平为单级列索引,并为df ['label']创建pd.Series数据类型,然后允许使用字符串访问器和contains