5 examples of 'select specific columns in pandas' in Python

Every line of 'select specific columns in pandas' 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
197def select_column(df, token):
198 column_name = token.value
199 if "." in column_name:
200 table_name, column_name = column_name.split(".", 1)
201 #TODO: Assert table name is correct
202 #TODO: Catch KeyError exception and raise error saying column does not exist
203 return df[column_name]
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

42def extractCols(df, colnames):
43 extracted = df[colnames]
44 df.drop(extracted.columns, axis=1, inplace=True)
45 return extracted
4119def __getitem__(self, key):
4120 if not isinstance(key, tuple):
4121 key = (key,)
4122 if len(self._internal._index_map) < len(key):
4123 raise KeyError("Key length ({}) exceeds index depth ({})"
4124 .format(len(key), len(self._internal.index_map)))
4125
4126 cols = (self._internal.index_scols[len(key):] +
4127 [self._internal.scol_for(self._internal.column_index[0])])
4128 rows = [self._internal.scols[level] == index
4129 for level, index in enumerate(key)]
4130 sdf = self._internal.sdf \
4131 .select(cols) \
4132 .where(reduce(lambda x, y: x & y, rows))
4133
4134 if len(self._internal._index_map) == len(key):
4135 # if sdf has one column and one data, return data only without frame
4136 pdf = sdf.limit(2).toPandas()
4137 length = len(pdf)
4138 if length == 1:
4139 return pdf[self.name].iloc[0]
4140
4141 key_string = name_like_string(key)
4142 sdf = sdf.withColumn(SPARK_INDEX_NAME_FORMAT(0), F.lit(key_string))
4143 internal = _InternalFrame(sdf=sdf, index_map=[(SPARK_INDEX_NAME_FORMAT(0), None)])
4144 return _col(DataFrame(internal))
4145
4146 internal = self._internal.copy(
4147 sdf=sdf,
4148 index_map=self._internal._index_map[len(key):])
4149
4150 return _col(DataFrame(internal))
1006def select_dtypes(self, include=None, exclude=None):
1007 start = time.time()
1008 init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
1009
1010 _select_dtypes = self._data.select_dtypes(include, exclude)
1011
1012 finish = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
1013 self._last_operation_dict['time'] = time.time() - start
1014 self._last_operation_dict['name'] = 'select_dtypes'
1015 self._last_operation_dict['mem_usage'] = finish - init
1016 return _select_dtypes
50def get_continuous_columns(X):
51 """Get all continuous features from a pandas DataFrame.
52
53 This function selects all numeric columns from a pandas
54 DataFrame that are within the ``float`` family.
55
56 Parameters
57 ----------
58 X : pd.DataFrame
59 The input dataframe.
60 """
61 return X.select_dtypes(include=[float])

Related snippets