Pandas Dataframe rolling with two columns and two rows

后端 未结 1 1878
天涯浪人
天涯浪人 2021-01-18 13:24

I got a dataframe with two columns that are holding Longitude and Latitude coordinates:

import pandas as pd

values = {\'Latitude\': {0: 47.0215033656         


        
相关标签:
1条回答
  • 2021-01-18 13:36

    Since pandas v0.23 it is now possible to pass a Series instead of a ndarray to Rolling.apply(). Just set raw=False.

    raw : bool, default None

    False : passes each row or column as a Series to the function.

    True or None : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance. The raw parameter is required and will show a FutureWarning if not passed. In the future raw will default to False.

    New in version 0.23.0.

    So building on your given example, you could move the latitude to the index and pass the whole longitude series---including the index---to your function:

    df = df.set_index('Latitude')
    df['Distance'] = df['Longitude'].rolling(2).apply(haversine_distance, raw=False)
    
    0 讨论(0)
提交回复
热议问题