9 examples of 'merge multiple dataframes in pandas' in Python

Every line of 'merge multiple dataframes 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
813def merge(old_cols, new_cols):
814 return old_cols + new_cols
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

57def merge(left, right):
58 return left.merge(right, on=on, how=how, suffixes=(lsuffix, rsuffix))
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))
24def _merge_dataframes(self, dataset_readers_list):
25
26 final_df = None
27 all_dfs = []
28 keys = []
29
30 for dataset, readers in dataset_readers_list:
31 dataset_df = readers[0]
32 for df in readers[1:]:
33 if df is None:
34 continue
35 dataset_df = dataset_df.add(df, fill_value=0.)
36
37 all_dfs.append(dataset_df)
38 keys.append(dataset)
39
40 final_df = pd.concat(all_dfs, keys=keys, names=['dataset'], sort=True)
41
42 return final_df.reset_index()
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
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()
6def default_merger(x, y):
7 import pandas as pd
8 return pd.concat([x, y])
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
42def merge_features(features, keep_partial_nan_rows=False):
43 """
44 Combine dataframes of features which share a datetime index.
45
46 Parameters
47 ----------
48 features : :any:`list` of :any:`pandas.DataFrame`
49 List of dataframes to be concatenated to share an index.
50 keep_partial_nan_rows : :any:`bool`, default False
51 If True, don't overwrite partial rows with NaN, otherwise any row with a NaN
52 value gets changed to all NaN values.
53
54 Returns
55 -------
56 merged_features : :any:`pandas.DataFrame`
57 A single dataframe with the index of the input data and all of the columns
58 in the input feature dataframes.
59 """
60
61 def _to_frame_if_needed(df_or_series):
62 if isinstance(df_or_series, pd.Series):
63 return df_or_series.to_frame()
64 return df_or_series
65
66 df = pd.concat([_to_frame_if_needed(feature) for feature in features], axis=1)
67
68 if not keep_partial_nan_rows:
69 df = overwrite_partial_rows_with_nan(df)
70 return df

Related snippets