8 examples of 'merge in pandas' in Python

Every line of 'merge 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
29def _concat(df, type):
30 if df is None:
31 df = pd.DataFrame(_object_blocks[type])
32 else:
33 _df = pd.DataFrame(_object_blocks[type])
34 df = pd.concat([df, _df], sort=True)
35 return df
332def merge_datasets(self, other):
333 """
334 This operation combines two dataframes into one new DataFrame.
335 If the operation is combining two SpatialDataFrames, the
336 geometry_type must match.
337 """
338 if isinstance(other, SpatialDataFrame) and \
339 other.geometry_type == self.geometry_type:
340 return pd.concat(objs=[self, other], axis=0)
341 elif isinstance(other, DataFrame):
342 return pd.concat(objs=[self, other], axis=0)
343 elif isinstance(other, Series):
344 self['merged_datasets'] = other
345 elif isinstance(other, SpatialDataFrame) and \
346 other.geometry_type != self.geometry_type:
347 raise ValueError("Spatial DataFrames must have the same geometry type.")
348 else:
349 raise ValueError("Merge datasets cannot merge types %s" % type(other))
6def default_merger(x, y):
7 import pandas as pd
8 return pd.concat([x, y])
813def merge(old_cols, new_cols):
814 return old_cols + new_cols
142def append_data(df1, df2):
143 '''
144 Append df2 to df1
145 '''
146 df = pd.concat((df1, df2))
147 return df.groupby(df.index).first()
51def random_merge(A, B, N=20, on='AnswerId', key='key', n='n'):
52 """Pair all rows of A with 1 matching row on "on" and N-1 random rows from B
53 """
54 assert key not in A and key not in B
55 X = A.copy()
56 X[key] = A[on]
57 Y = B.copy()
58 Y[key] = B[on]
59 match = X.merge(Y, on=key).drop(key, axis=1)
60 match[n] = 0
61 df_list = [match]
62 for i in A.index:
63 X = A.loc[[i]]
64 Y = B[B[on] != X[on].iloc[0]].sample(N-1)
65 X[key] = 1
66 Y[key] = 1
67 Z = X.merge(Y, how='outer', on=key).drop(key, axis=1)
68 Z[n] = range(1, N)
69 df_list.append(Z)
70 df = pd.concat(df_list, ignore_index=True)
71 return df
474def full_merge_of_existing_series(old_series, new_series):
475 """
476 Merges old data with new data.
477 Any Nan in the existing data will be replaced (be careful!)
478
479 :param old_data: pd.Series
480 :param new_data: pd.Series
481
482 :returns: pd.Series
483 """
484 if len(old_series)==0:
485 return new_series
486 if len(new_series)==0:
487 return old_series
488
489 joint_data = pd.concat([old_series, new_series], axis=1)
490 joint_data.columns = ['original', 'new']
491
492 # fill to the left
493 joint_data_filled_across = joint_data.bfill(1)
494 merged_data = joint_data_filled_across['original']
495
496 return merged_data
88def merge_lvj_dfs(df1, df2, how='outer'):
89 """
90 Merge two data frames on lvj indices.
91
92 By default, uses the union of the keys (an "outer" join).
93 """
94 merged = pd.merge(df1, df2, how=how, left_index=True, right_index=True)
95 merged.fillna(0, inplace=True)
96 return merged

Related snippets