相当具体的数据框架查询

问题描述 投票:0回答:1
#=================================================================================
# Building the dataframe (No Problems Here)
#=================================================================================  
plhIndex = pd.DataFrame(columns=['fullPath', 'folderName', 'fileName', 'modifiedDate'])
for root, dirs, files in os.walk(processHistoryPath):
    for file in files:
        plhIndex = plhIndex.append({'fullPath' : os.path.join(root, file), \
                                    'folderName' : os.path.basename(root), \
                                    'fileName' : file, \
                                    'modifiedDate' : os.path.getmtime(os.path.join(root, file))}, ignore_index=True)

我需要的是一个从 plhIndex 中返回'fullPath'和'FolderName'的函数。

A) 文件名与我提供的变量相匹配。

B) modifiedDate与我提供的函数中的日期最接近(均为时间戳格式)

类似于:return min(plhIndex , key=lambda x:abs(date-modifiedDateColumn))但序列号匹配的地方。

衷心感谢大家的帮助!

python pandas dataframe
1个回答
0
投票

獨立調查組。

def get_closest(fn, date, df):
    d = pd.DataFrame({'fileName': [fn], 'modifiedDate': [date]})
    a = pd.merge_asof(d, df, on='modifiedDate', by='fileName', direction='nearest')
    return a.loc[0, ['fullPath', 'folderName']].to_dict()

这样叫吧。

get_closest('myfilename', some_date, plhIndex)
© www.soinside.com 2019 - 2024. All rights reserved.