7 examples of 'pandas split dataframe by column value' in Python

Every line of 'pandas split dataframe by column value' 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
253def _split_dataframe(df, dense_columns):
254 """Split a DataFrame by creating groups of the same values for the dense dims."""
255 groups = {name: group for name, group in df.groupby(dense_columns)}
256 groups = convert_dictionary_keys_to_dense_indices(groups)
257
258 return groups
4def split_and_expand(df, col, sep):
5 split_col = df[col].str.split(sep).apply(pd.Series, 1).stack()
6 split_col.index = split_col.index.droplevel(-1)
7 split_col.name = col
8 df = df.drop(col, axis=1).join(split_col)
9 df.reset_index(drop=True, inplace=True)
10 return df
341def split_frame(self, index):
342 """
343 Split data frame by an index
344
345 Parameters
346 ----------
347 index : string
348 used to sep the DataObject into groups
349
350 Returns
351 -------
352 tuple of DataObjects
353
354 Examples
355 --------
356 >>> df = DataObject([(6, 'APL'), (2, 'IBM')])
357 >>> df1, df2 = df.split_frame('a')
358 >>> df1.to_dict()
359 {'b': {2: 'IBM'}}
360 >>> df2.to_dict()
361 {'b': {6: 'APL'}}
362 """
363 df = self.unindexed.set_index(index)
364 g = [DataObject(g[1]) for g in df.groupby(level=0)]
365 return tuple(g)
167@timeit
168def split_X_y(df: pd.DataFrame, config: Config) -> (pd.DataFrame, Optional[pd.Series]):
169 if config['params']['field_target_name'] in df.columns:
170 return df.drop(config['params']['field_target_name'], axis=1), df[config['params']['field_target_name']]
171 else:
172 return df, None
2558@Appender(_shared_docs["str_split"] % {"side": "beginning", "method": "split"})
2559@forbid_nonstring_types(["bytes"])
2560def split(self, pat=None, n=-1, expand=False):
2561 result = str_split(self._parent, pat, n=n)
2562 return self._wrap_result(result, expand=expand)
6def split(df):
7 '''
8
9 :param df: Dataframe to be splited
10 :return: Sorted list of dataframe's splited list
11 '''
12 trainingSet, testSet = train_test_split(df, test_size=0.2)
13 sorted_trainSet = trainingSet.sort_values('user_id')
14 sorted_testSet = testSet.sort_values('user_id')
15 return sorted_testSet, sorted_trainSet
163def split(self, index_series, proportion, batch_size=None):
164 """Deterministically split a `DataFrame` into two `DataFrame`s.
165
166 Note this split is only as deterministic as the underlying hash function;
167 see `tf.string_to_hash_bucket_fast`. The hash function is deterministic
168 for a given binary, but may change occasionally. The only way to achieve
169 an absolute guarantee that the split `DataFrame`s do not change across runs
170 is to materialize them.
171
172 Note too that the allocation of a row to one partition or the
173 other is evaluated independently for each row, so the exact number of rows
174 in each partition is binomially distributed.
175
176 Args:
177 index_series: a `Series` of unique strings, whose hash will determine the
178 partitioning; or the name in this `DataFrame` of such a `Series`.
179 (This `Series` must contain strings because TensorFlow provides hash
180 ops only for strings, and there are no number-to-string converter ops.)
181 proportion: The proportion of the rows to select for the 'left'
182 partition; the remaining (1 - proportion) rows form the 'right'
183 partition.
184 batch_size: the batch size to use when rebatching the left and right
185 `DataFrame`s. If None (default), the `DataFrame`s are not rebatched;
186 thus their batches will have variable sizes, according to which rows
187 are selected from each batch of the original `DataFrame`.
188
189 Returns:
190 Two `DataFrame`s containing the partitioned rows.
191 """
192 if isinstance(index_series, str):
193 index_series = self[index_series]
194 left_mask, = split_mask.SplitMask(proportion)(index_series)
195 right_mask = ~left_mask
196 left_rows = self.select_rows(left_mask)
197 right_rows = self.select_rows(right_mask)
198
199 if batch_size:
200 left_rows = left_rows.batch(batch_size=batch_size, shuffle=False)
201 right_rows = right_rows.batch(batch_size=batch_size, shuffle=False)
202
203 return left_rows, right_rows

Related snippets