panda 如何判断一个关键字的所在列

import pandas as pd
df = pd.DataFrame({'key1':list('abcde'),'key2':['one','two','three','four','five']})

search_key = ['a']

if sum(df.isin(search_key).sum()):
    #首先判断是否存在
    col_name = df.columns[[df.isin(search_key).sum().tolist().index(i) for i in df.isin(search_key).sum().tolist() if i != 0]]
    print(col_name)
else:
    print('要查找的关键字不在DataFrame里')

你可能感兴趣的:(panda 如何判断一个关键字的所在列)