Python 用于获取数据帧值位置的自定义函数

Python 用于获取数据帧值位置的自定义函数,python,dataframe,Python,Dataframe,这个问题和我的上一个问题差不多,但差别不大,我有一个DF Index Batch Name List Name 0 1 Jon Adam 1 2 2 Adam Sam 3 Chris 4 3 Voges Jon 5 6 4 Jon Voges 我想搜索列表名中每个值的批号,

这个问题和我的上一个问题差不多,但差别不大,我有一个DF

Index   Batch   Name    List Name
0        1      Jon     Adam
1           
2        2      Adam    Sam
3                       Chris
4        3      Voges   Jon
5           
6        4      Jon     Voges
我想搜索列表名中每个值的批号,即Adam、Sam、Chris、Jon和Voges。我想要另一个DF,如下所示

Index   Batch   Name    List Name   BatchNames
0        1      Jon     Adam        Adam(2)
1               
2        2      Adam    Sam         Sam(2)
3                       Chris       Chris(2)
4        3     Voges    Jon         Jon(1,4)
5               
6        4     Jon      Voges       Voges(3)

我想选择每个列表名称,并在名称中搜索相应的批号,即
Jon
存在于
1和4中,依此类推。但是,如果Listname中的名称在名称中不存在,则应选择与其接近的相应批号,例如,
Sam
在名称中不存在,但它接近
Batch 2
Chris
。基本上,批次之间存在的任何物质都属于最低批号。如何为此编写自定义函数

我会这样做:

import pandas as pd
import numpy as np

def custom_function(df):
    # Forward fill the Batch number
    df_Batch = df.Batch.copy()
    df.Batch.ffill(inplace=True)
    df.Batch = df.Batch.astype(int)
    # Make a new dataframe where we first get batches for the name column
    # and append batches for the list name column, there we be duplicates so we keep the first entry
    a = df.groupby('Name').Batch.apply(tuple).append(df.groupby('List Name').Batch.apply(tuple)).reset_index().groupby('index').first()
    # Create a series which concatenates the Batch number and List Name
    b = pd.Series(a.index.astype(str) + a.Batch.astype(str), index=a.index).replace(',','', regex=True).replace(' ',',',regex=True)
    # undo the forward fill (replace with original columns)
    df.Batch = df_Batch
    # join the series we just made to the dataframe
    return df.merge(b.to_frame().rename_axis('List Name'), how='left', on='List Name', suffixes=['', 'Names']).fillna('')
df = pd.DataFrame({'Batch':[1,np.nan,2,np.nan,3,np.nan,4], 'Name':['Jon',np.nan, 'Adam',np.nan, 'Voges',np.nan, 'Jon'], 'List Name':['Adam', np.nan, 'Sam', 'Chris', 'Jon', np.nan, 'Voges']})
# Out[122]: 
#    Batch   Name List Name
# 0    1.0    Jon      Adam
# 1    NaN    NaN       NaN
# 2    2.0   Adam       Sam
# 3    NaN    NaN     Chris
# 4    3.0  Voges       Jon
# 5    NaN    NaN       NaN
# 6    4.0    Jon     Voges
custom_function(df)
# Out[131]: 
#   Batch   Name List Name BatchNames
# 0     1    Jon      Adam    Adam(2)
# 1                                  
# 2     2   Adam       Sam     Sam(2)
# 3                  Chris   Chris(2)
# 4     3  Voges       Jon   Jon(1,4)
# 5                                  
# 6     4    Jon     Voges   Voges(3)

我在pandas中得到一个错误文件“pandas_libs\index.pyx”,第117行,在pandas中,_libs.index.IndexEngine.get_loc文件“pandas_libs\index.pyx”,第139行,在pandas中,_libs.index.IndexEngine.get_loc文件“pandas_libs\hashtable\u class\u helper.pxi”,在pandas中第1265行,_libs.hashtable.PyObjectHashTable.get_项文件“pandas\u libs\hashtable\u class\u helper.pxi,第1273行,在pandas中。_libs.hashtable.PyObjectHashTable.get_item keyrerror:“List Name”如果您只是复制并粘贴到python shell/解释器中,它是否起作用?如果没有,可能会有一些版本问题,我想知道您使用的是哪个pandas/python版本。如果它真的有效,你能发布完整的回溯和/或你正在运行定义的数据帧的可复制副本吗(这样我就可以调试它)?我运行了你给我的确切代码,我使用的是python 3.6,甚至创建了与你在这里相同的DF,当我转到a并复制和粘贴代码时,我得到了上述错误,它按预期运行。你用的是什么版本的熊猫?i、 e.
pip显示熊猫返回的内容是什么?版本:0.22.0