3 examples of 'pandas find value in any column' in Python

Every line of 'pandas find value in any column' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
63def find(columns: list, col_name: str):
64 """
65 Retrieve column by name.
66 :param columns: columns to search
67 :param col_name: name of column to return
68 :returns: column
69 """
70 try:
71 return next(iter([col for col in columns if col.get_name() == col_name]))
72 except StopIteration:
73 print("column '{}' not found in {}".format(col_name, [c.get_name() for c in columns]))
74 return None
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

78def search(df, match, columns=['Proteins','Protein names','Gene names']):
79 """
80 Search for a given string in a set of columns in a processed ``DataFrame``.
81
82 Returns a filtered ``DataFrame`` where `match` is contained in one of the `columns`.
83
84 :param df: Pandas ``DataFrame``
85 :param match: ``str`` to search for in columns
86 :param columns: ``list`` of ``str`` to search for match
87 :return: filtered Pandas ``DataFrame``
88 """
89 df = df.copy()
90 dft = df.reset_index()
91
92 mask = np.zeros((dft.shape[0],), dtype=bool)
93 idx = ['Proteins','Protein names','Gene names']
94 for i in idx:
95 if i in dft.columns:
96 mask = mask | np.array([match in str(l) for l in dft[i].values])
97
98 return df.iloc[mask]
120def get_column_data(self, item):
121 item = to_text(item)
122 if item not in self.names:
123 return None
124 if self._pandas:
125 if item in self.values.index.names:
126 return self.values.index.get_level_values(item)
127 else:
128 return self.values[item]
129 else:
130 col_id = list(idx for idx, c in self.names if c == item)[0]
131 return [r[col_id] for r in self.values]

Related snippets