Python Forum
iretate over columns in df and calculate euclidean distance with one column in pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iretate over columns in df and calculate euclidean distance with one column in pandas
Hi, I have a dataset with several columns (Time Series) and I would like to synchronize them - the 'col2' should be the reference.

[Image: dzJE1.png]

Here is my df:

[Image: VDXo9.png]

With the code below I am able to synchronize the only two columns 'col3' according to 'col2' (time series).

import pandas as pd
import numpy as np
# pip install fastdtw  

df=pd.DataFrame({'ID':range(0,25), 'col2':np.random.randn(25)+3, 'col3':np.random.randn(25)+3,'col4':np.random.randn(25)+3,'col5':np.random.randn(25)+3})
from fastdtw import *
from scipy.spatial.distance import *

x = np.array(df['col2'].fillna(0))
y = np.array(df['col3'].fillna(0))

distance, path = fastdtw(x, y, dist=euclidean)

result = []

for i in range(0,len(path)):
df_synchronized = pd.DataFrame(data=result,columns=['ID','col2','col3']).dropna()
df_synchronized = df_synchronized.drop_duplicates(subset=['ID'])
df_synchronized = df_synchronized.sort_values(by='ID')
df_synchronized = df_synchronized.reset_index(drop=True)

Here is the df_synchronized:
[Image: 0SpmE.png]

I would like to iterate over all columns in DataFrame and do the same for 'col4' and 'col5' as was for 'col3' being done. Simply, 'col3' needs to be replaced in a loop with 'col4' and 'col5'. The goal would be to have the df_synchronized with all columns from df.

Is there any way, how to make it done?

distance, path = fastdtw(x, y, dist=euclidean)

can't be change to distance, path = fastdtw(x, y, z, aa, dist=euclidean). 'Synchronization' needs to be done on one column, then save into df_synchronized, then with next column...
Yoriz write May-09-2021, 06:52 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 2 101 7 hours ago
Last Post: mbrown009
  pandas column percentile nuncio 7 1,756 Aug-10-2022, 04:41 AM
Last Post: nuncio
  Pandas dataframe: calculate metrics by year mcva 1 1,860 Mar-02-2022, 08:22 AM
Last Post: mcva
  pandas: Compute the % of the unique values in a column JaneTan 1 1,520 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 2,257 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  How to move each team row to a new column. Pandas vladiwnl 0 1,546 Jun-13-2021, 08:10 AM
Last Post: vladiwnl
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 2,214 Feb-20-2021, 08:25 PM
Last Post: perfringo
  Remove extra count columns created by pandas groupby spyf8 1 2,324 Feb-10-2021, 09:19 AM
Last Post: Naheed
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,623 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Pandas: summing columns conditional on the column labels ddd2332 0 1,883 Sep-10-2020, 05:58 PM
Last Post: ddd2332

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020