在Pandas中搜索字符串值

时间:2017-04-13 09:24:29

标签: python pandas

我正在尝试在Pandas数据框中搜索值。

这就是我的DF的样子:

                                                       0          1            2   \
0                                                 NaN        NaN          NaN   
1              CITI Pass-T...                     NaN        NaN   
2                                                 NaN        NaN          NaN   
3              Certificateholder Distribution Summary        NaN          NaN   
4                                               Class      CUSIP  Record Date   
5                                                 A-1  25151EAA1   12/30/2016   
6                                                 A-2  25151EAB9   12/30/2016   
7                                                 A-3  25151EAC7   12/30/2016   
8                                                 A-4  25151EAD5   12/30/2016   
9                                                A-5A  25151EAE3   12/30/2016   
10                                               A-5B  25151EAF0   12/30/2016   
11                                                A-6  25151EAG8   12/30/2016   
12                                                A-7  25151EAH6   12/30/2016   
13                                                A-8  25151EAJ2   01/24/2017   
14                                                M-1  25151EAK9   12/30/2016   
15                                                M-2  25151EAL7   12/30/2016   
16                                                M-3  25151EAM5   12/30/2016   
17                                                M-4  25151EAN3   12/30/2016   
18                                                M-5  25151EAP8   12/30/2016   
19                                                M-6  25151EAQ6   12/30/2016   
20                                                M-7  25151EAR4   12/30/2016   
21                                                M-8  25151EAS2   12/30/2016   
22                                                M-9  25151EAT0   12/30/2016   
23                                               M-10  25151EAU7   12/30/2016   
24                                               M-11  25151EAV5   12/30/2016   
25                                                  P  25151EAX1   12/30/2016   
26                                                 CE  25151EAW3   12/30/2016   
27                                                  R  25151EAY9   12/30/2016   
28                                             Totals        NaN          NaN   
29  This report is compiled by me, N...                      NaN          NaN   
30  All Record Dates are based upon the governing ...        NaN          NaN   
31                                                NaN        NaN          NaN  

所以你看,没有真正的列标题。 现在我想要搜索值A-1。

这就是我所做的:

for col in df:
    print col
    print df[df[col].str.contains("A-1", na=False)]

这实际上给了我我想要的结果:

    0          1           2     3           4        5          6         7   \
5  A-1  25151EAA1  12/30/2016  6.25  7218381.58  25379.0  143237.93  71982.98   

           8          9           10  11  12  13  14  
5  7003160.66  168616.93  6169381.87 NaN NaN NaN NaN  

但后来我收到以下错误:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

有人知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

我试一试,你可以检查列是否为空:

for col in df:
    if not df[col].empty:
        print col
        print df[df[col].str.contains("A-1", na=False)]