熊猫:应用搜索功能

时间:2017-09-08 10:41:00

标签: python regex python-3.x pandas dataframe

我正在尝试在自己的数据上重现一些neat forest plots。但是,我坚持这个功能,不能为我的生活弄清楚它应该做什么。

我正在努力使以下代码适用于我的数据:

  featval      mean        sd  mc_error   hpd_2.5  hpd_97.5
0    mu_a -0.008913  0.011715  0.000613 -0.029139  0.014329
1    mu_b  0.003252  0.000271  0.000015  0.002698  0.003765
2    a__0 -0.065255  0.024315  0.001168 -0.113708 -0.018885
3    a__1 -0.081748  0.023247  0.001114 -0.124560 -0.036777
4    a__2  0.025326  0.021661  0.001024 -0.019744  0.065263

打印返回的地方:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-125-2465af1d68b8> in <module>()
----> 1 dfsm_unpl_mfr = create_smry(hierarchical_trace[-333:], data, 'subject')
      2 custom_forestplot(dfsm_unpl_mfr)

<ipython-input-123-5f6828d6cf8e> in create_smry(trc, data, pname)
      8 
      9     dfsm = dfsm.loc[dfsm['featval'].apply(
---> 10         lambda x: re.search('{}__[0-9]+'.format(pname), x) is not None)]
     11 
     12     dfsm.set_index(dfs[pname].unique(), inplace=True)

~/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   2353             else:
   2354                 values = self.asobject
-> 2355                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2356 
   2357         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer (pandas/_libs/lib.c:66645)()

<ipython-input-123-5f6828d6cf8e> in <lambda>(x)
      8 
      9     dfsm = dfsm.loc[dfsm['featval'].apply(
---> 10         lambda x: re.search('{}__[0-9]+'.format(pname), x) is not None)]
     11 
     12     dfsm.set_index(dfs[pname].unique(), inplace=True)

NameError: name 're' is not defined

错误:

{}__[0-9]+
  1. 我无法弄清楚re.search是什么,因为re不是df。
  2. import re def create_smry(trc, data, pname='subject'): ''' Conv fn: create trace summary for sorted forestplot ''' dfsm = pm.df_summary(trc).reset_index() dfsm.rename(columns={'index':'featval'}, inplace=True) print(dfsm.head(n=10)) dfsm = dfsm.loc[dfsm['featval'].apply( lambda x: re.search('{}__[0-90]+'.format(pname), x) is not None)] print(dfsm.head(n=10)) dfsm.set_index(data[pname].unique(), inplace=True) dfsm.sort_values('mean', ascending=True, inplace=True) dfsm['ypos'] = np.arange(len(dfsm)) print(dfsm.head(n=15)) return dfsm 在此背景下的含义是什么?
  3. 由于输入非常复杂,我无法提供最小的工作示例。

    导入正则表达式后:

      featval      mean        sd  mc_error   hpd_2.5  hpd_97.5
    0   b0_mu -0.022521  0.010266  0.000597 -0.042222 -0.003072
    1   b1_mu  0.003220  0.000256  0.000014  0.002742  0.003700
    2   b2_mu  0.024366  0.005288  0.000292  0.014786  0.035139
    3   b3_mu  0.008563  0.004393  0.000243  0.000634  0.017385
    4   b0__0 -0.078060  0.025093  0.001208 -0.121480 -0.024921
    5   b0__1 -0.097636  0.024500  0.001413 -0.144801 -0.052600
    6   b0__2  0.009216  0.024381  0.001229 -0.038927  0.052254
    7   b0__3  0.024541  0.025525  0.001399 -0.025824  0.070295
    8   b0__4 -0.069331  0.020887  0.001057 -0.106392 -0.024169
    9   b0__5 -0.065629  0.024787  0.001178 -0.111582 -0.019849
    Empty DataFrame
    Columns: [featval, mean, sd, mc_error, hpd_2.5, hpd_97.5]
    Index: []
    

    返回

    dfsm['featidx'] = dfsm['featval'].apply(lambda x: any(pd.Series(x).str.contains(feat)))
    

    如果我阻止re.search并简单地绘图(也不要尝试更改索引,我得到一个情节:

    enter image description here

    但是,re.search未正确使用,因此绘制了trc fra的所有y值。

    编辑:结束使用

    url='http://ssl.gstatic.com/dictionary/static/sounds/oxford/'    
    audio=requests.get(url+'lucid'+'--_gb_1.mp3', stream=True).content
    with open('lucid'+'.mp3', 'wb') as f:
         f.write(audio)
    

    因为我无法弄清楚正则表达式。

1 个答案:

答案 0 :(得分:2)

I cant figure out what re.search is, since re is not a df.

re是一个正则表达式库,用于对字符串执行正则表达式操作。您需要在头文件或python文件中调用import re才能使用它。

What does {}__[0-9]+ mean in this context?

这是一个正则表达式模式,re.search表示,扫描字符串,查找此正则表达式({}__[0-9]+)产生匹配的位置,并返回相应的匹配对象。

有关该库的更多信息: 'Regex Documentation'