Python 查找多个数据帧中是否存在列值

Python 查找多个数据帧中是否存在列值,python,pandas,Python,Pandas,我有4个excel文件-“a1.xlsx”,“a2.xlsx”,“a3.xlsx”,“a4.xlsx” 文件的格式相同 对于例如a1.xlsx,其如下所示: id code name 1 100 abc 2 200 zxc ... ... ... 我必须在pandas dataframe中读取此文件,并检查多个excel文件中是否存在相同的code列值 像这样的 如果code=100存在于'a1.xlsx'、'a3.xlsx'中,并且co

我有4个excel文件-“a1.xlsx”,“a2.xlsx”,“a3.xlsx”,“a4.xlsx” 文件的格式相同

对于例如a1.xlsx,其如下所示:

id    code    name
1      100    abc
2      200    zxc
...    ...    ...
我必须在pandas dataframe中读取此文件,并检查多个excel文件中是否存在相同的
code
列值

像这样的

如果
code=100
存在于
'a1.xlsx'、'a3.xlsx'
中,并且
code=200
仅存在于
'a1.xlsx'

最终数据帧应如下所示:

code    filename
100   a1.xlsx,a3.xlsx
200   a1.xlsx
...   ....
and so on
我将所有文件都放在一个目录中,并尝试通过循环对它们进行迭代

import pandas as pd
import os
x = next(os.walk('path/to/files/'))[2]  #list all files in directory
os.chdir('path/to/files/')

for i in range (0,len(x)):
    df = pd.read_excel(x[i])
如何进行?有线索吗

使用:

import glob 

#get all filenames 
files = glob.glob('path/to/files/*.xlsx')
#list comprehension with assign new column for filenames
dfs = [pd.read_excel(fp).assign(filename=os.path.basename(fp).split('.')[0]) for fp in files]
#one big df from list of dfs
df = pd.concat(dfs, ignore_index=True)
#join all same codes
df1 = df.groupby('code')['filename'].apply(', '.join).reset_index()