w3resource

Pandas Series: reindex_like() function

Object with matching indices as other object in Pandas

The reindex_like() function is used to get an object with matching indices as other object.

Conform the object to the same index on all axes. Optional filling logic, placing NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False.

Syntax:

Series.reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None)
Pandas Series reindex_like image

Parameters:

Name Description Type/Default Value Required / Optional
other Its row and column indices are used to define the new indices of this object. Object of the same data type optional
method Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.
  • None (default): don’t fill gaps
  • pad / ffill: propagate last valid observation forward to next valid
  • backfill / bfill: use next valid observation to fill gap
  • nearest: use nearest valid observations to fill gap
{None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’} Required
copy Return a new object, even if the passed indexes are the same. bool
Default Value: True
Required
limit Maximum number of consecutive labels to fill for inexact matches. bool
Default Value: None
Required
tolerance Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations most satisfy the equation abs(index[indexer] - target) <= tolerance.
Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like includes list, tuple, array, Series, and must be the same size as the index and its dtype must exactly match the index’s type.
optional

Returns: Series or DataFrame
Same type as caller, but with changed indices on each axis.

NotesSame as calling .reindex(index=other.index, columns=other.columns,...).

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df1 = pd.DataFrame([[34.3, 77.7, 'high'],
                    [32, 88.8, 'high'],
                    [24, 72.6, 'medium'],
                    [31, 95, 'medium']],
    columns=['temp_celsius', 'temp_fahrenheit', 'windspeed'],
    index=pd.date_range(start='2019-04-12',
                        end='2019-04-15', freq='D'))
df1

Output:

            temp_celsius	temp_fahrenheit	windspeed
2019-04-12	     34.3	    77.7	        high
2019-04-13	     32.0	    88.8	        high
2019-04-14	     24.0	    72.6	       medium
2019-04-15	     31.0	    95.0	       medium

Python-Pandas Code:

import numpy as np
import pandas as pd
df2 = pd.DataFrame([[27, 'low'],
                    [28, 'low'],
                    [29.2, 'medium']],
    columns=['temp_celsius', 'windspeed'],
    index=pd.DatetimeIndex(['2019-04-12', '2019-04-13',
                            '2019-04-15']))
df2

Output:

            temp_celsius	windspeed
2019-04-12	     27.0	       low
2019-04-13	     28.0	       low
2019-04-15	     29.2	     medium

Python-Pandas Code:

import numpy as np
import pandas as pd
df2 = pd.DataFrame([[27, 'low'],
                    [28, 'low'],
                    [29.2, 'medium']],
    columns=['temp_celsius', 'windspeed'],
    index=pd.DatetimeIndex(['2019-04-12', '2019-04-13',
                            '2019-04-15']))
df2.reindex_like(df1)

Output:

            temp_celsius	temp_fahrenheit	windspeed
2019-04-12	      27.0	        NaN	         low
2019-04-13	      28.0	        NaN	         low
2019-04-14	      NaN	        NaN	         NaN
2019-04-15	      29.2	        NaN	       medium

Previous: Conform series in Pandas
Next: Alter Series index in Pandas



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-reindex_like.php