Pandas:应用搜索功能

2024-05-16 22:49:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我自己的数据上重现一些neat forest plots。然而,我被困在这个功能,并不能为我的生活,找出它应该做什么。你知道吗

我正在尝试对我的数据使用以下代码:

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=5))

    dfsm = dfsm.loc[dfsm['featval'].apply(
        lambda x: re.search('{}__[0-9]+'.format(pname), x) is not None)]

    dfsm.set_index(dfs[pname].unique(), inplace=True)
    dfsm.sort('mean', ascending=True, inplace=True)
    dfsm['ypos'] = np.arange(len(dfsm))

    return dfsm

如果打印返回:

  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
  1. 我想不出是什么检索是,因为re不是df。
  2. 在这种情况下{}__[0-9]+是什么意思?

由于输入非常复杂,我不能提供一个最小的工作示例。你知道吗

导入regex后:

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

它回来了

  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: []

如果我挡住了检索简单地绘制(也不要试图改变索引,我得到一个图:

enter image description here

但是,检索未正确使用,因此绘制trc fra的所有y值。你知道吗

编辑:最终使用

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

因为我搞不懂正则表达式。你知道吗


Tags: lambdaretruedataindexcreateb0apply
1条回答
网友
1楼 · 发布于 2024-05-16 22:49:22

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?

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

有关库的详细信息: 'Regex Documentation'

相关问题 更多 >