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
#1
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)):
    result.append([df['ID'].iloc[path[i][0]],
    df['col2'].iloc[path[i][0]],
    df['col3'].iloc[path[i][1]]])
    
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)
df_synchronized.head(n=3) 
-------------
-------------

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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas column percentile nuncio 6 248 Aug-04-2022, 02:25 PM
Last Post: jefsummers
  Pandas dataframe: calculate metrics by year mcva 1 1,178 Mar-02-2022, 08:22 AM
Last Post: mcva
  pandas: Compute the % of the unique values in a column JaneTan 1 975 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 1,435 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  How to move each team row to a new column. Pandas vladiwnl 0 1,054 Jun-13-2021, 08:10 AM
Last Post: vladiwnl
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 1,539 Feb-20-2021, 08:25 PM
Last Post: perfringo
  Remove extra count columns created by pandas groupby spyf8 1 1,675 Feb-10-2021, 09:19 AM
Last Post: Naheed
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 1,808 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Pandas: summing columns conditional on the column labels ddd2332 0 1,368 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Pandas DataFrame and unmatched column sritsv19 0 2,221 Jul-07-2020, 12:52 PM
Last Post: sritsv19

Forum Jump:

User Panel Messages

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