如何在 pandas 数据框中的特定列中搜索字符串值,如果存在,则给出数据框中存在的该行的输出?

how to search a string value within a specific column in pandas dataframe, and if present, give an output of that row present in the dataframe?

我想搜索 .pkl 文件中的数据库。

我已加载 .pkl 文件并将其存储在名为 load_data 的变量中。

现在,我需要接受使用原始输入的字符串输入,并在我的数据集的一个特定列“SMILES”中搜索该字符串。

如果字符串匹配,我需要显示整行,即与该行对应的所有列值。

这可能吗?如果可以,我应该怎么做?

使用boolean indexing即returns所有匹配行:

df = pd.DataFrame({'a': [1,3,4],
                      'SMILES': ['a','dd b','f'],
                     'c': [1,2,0]})
print (df)
  SMILES  a  c
0      a  1  1
1   dd b  3  2
2      f  4  0

如果只需要检查一个字符串:

#raw_input for python 2, input for python 3
a = input('Enter String for SMILES columns: ') # f
#Enter String for SMILES columns: f
print (df[df['SMILES'] == a])
  SMILES  a  c
2      f  4  0

或者如果您需要检查子字符串,请使用 str.contains:

a = input('Enter String for SMILES columns: ') # b 
print (df[df['SMILES'].str.contains(a)])
#Enter String for SMILES columns: b
  SMILES  a  c
1   dd b  3  2