Ruslan Asadullin
Ruslan Asadullin

Reputation: 93

Pandas dataframe two loops task

I have two data frames with a different length: df and dfs. df index are day timestamps, dfs index are hour timestamps.

df:
...
2021-01-26 03:00:00+03:00    151.686333
2021-01-27 03:00:00+03:00    153.079667
2021-01-28 03:00:00+03:00    156.408000
...

df has 'atr' column, and I need to fill 'atr' column in dfs also if dfs day in timestamp is equal to df day timestamp. Two for-loops work correctly, buy they are just too slow. Is there any faster way to do this? Thank you in advance!

for t in dfs.index:
  for date in df.index:
    if t.day == date.day and t.month == date.month:
        dfs.loc[t, 'atr'] = df.loc[date, 'atr']

dfs:
...
2021-01-26 01:00:00+03:00    151.686
2021-01-26 02:00:00+03:00    151.686
2021-01-26 03:00:00+03:00    151.686
2021-01-26 04:00:00+03:00    151.686
2021-01-26 05:00:00+03:00    151.686
...
2021-01-27 00:00:00+03:00    153.08
2021-01-27 01:00:00+03:00    153.08
2021-01-27 02:00:00+03:00    153.08
2021-01-27 03:00:00+03:00    153.08
2021-01-27 04:00:00+03:00    153.08

Upvotes: 1

Views: 99

Answers (1)

Ruslan Asadullin
Ruslan Asadullin

Reputation: 93

It is done using lambda and apply:

def get_atr(x, df):
    atr = [df.loc[atr, 'atr']
           for atr in df.index
           if x.name.day == atr.day
           and x.name.month == atr.month][0]
           
    return atr

dfs['atr'] = dfs.apply(lambda x: get_atr(x, df), axis=1)

Upvotes: 1

Related Questions