- API reference
- General functions
- pandas.merge_ordered
pandas.merge_ordered#
- pandas.merge_ordered(left,right,on=None,left_on=None,right_on=None,left_by=None,right_by=None,fill_method=None,suffixes=('_x','_y'),how='outer')[source]#
Perform a merge for ordered data with optional filling/interpolation.
Designed for ordered data like time series data. Optionallyperform group-wise merge (see examples).
- Parameters:
- leftDataFrame or named Series
- rightDataFrame or named Series
- onlabel or list
Field names to join on. Must be found in both DataFrames.
- left_onlabel or list, or array-like
Field names to join on in left DataFrame. Can be a vector or list ofvectors of the length of the DataFrame to use a particular vector asthe join key instead of columns.
- right_onlabel or list, or array-like
Field names to join on in right DataFrame or vector/list of vectors perleft_on docs.
- left_bycolumn name or list of column names
Group left DataFrame by group columns and merge piece by piece withright DataFrame. Must be None if either left or right are a Series.
- right_bycolumn name or list of column names
Group right DataFrame by group columns and merge piece by piece withleft DataFrame. Must be None if either left or right are a Series.
- fill_method{‘ffill’, None}, default None
Interpolation method for data.
- suffixeslist-like, default is (“_x”, “_y”)
A length-2 sequence where each element is optionally a stringindicating the suffix to add to overlapping column names inleft andright respectively. Pass a value ofNone insteadof a string to indicate that the column name fromleft orright should be left as-is, with no suffix. At least one of thevalues must not be None.
- how{‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘outer’
left: use only keys from left frame (SQL: left outer join)
right: use only keys from right frame (SQL: right outer join)
outer: use union of keys from both frames (SQL: full outer join)
inner: use intersection of keys from both frames (SQL: inner join).
- Returns:
- DataFrame
The merged DataFrame output type will be the same as‘left’, if it is a subclass of DataFrame.
See also
merge
Merge with a database-style join.
merge_asof
Merge on nearest keys.
Examples
>>>frompandasimportmerge_ordered>>>df1=pd.DataFrame(...{..."key":["a","c","e","a","c","e"],..."lvalue":[1,2,3,1,2,3],..."group":["a","a","a","b","b","b"]...}...)>>>df1 key lvalue group0 a 1 a1 c 2 a2 e 3 a3 a 1 b4 c 2 b5 e 3 b
>>>df2=pd.DataFrame({"key":["b","c","d"],"rvalue":[1,2,3]})>>>df2 key rvalue0 b 11 c 22 d 3
>>>merge_ordered(df1,df2,fill_method="ffill",left_by="group") key lvalue group rvalue0 a 1 a NaN1 b 1 a 1.02 c 2 a 2.03 d 2 a 3.04 e 3 a 3.05 a 1 b NaN6 b 1 b 1.07 c 2 b 2.08 d 2 b 3.09 e 3 b 3.0