简体   繁体   English

Pandas dataframe 两个循环任务

[英]Pandas dataframe two loops task

I have two data frames with a different length: df and dfs.我有两个不同长度的数据帧:df 和 dfs。 df index are day timestamps, dfs index are hour timestamps. df 索引是日时间戳,dfs 索引是小时时间戳。

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. df 有 'atr' 列,如果时间戳中的 dfs 天等于 df 天时间戳,我也需要在 dfs 中填充 'atr' 列。 Two for-loops work correctly, buy they are just too slow.两个 for 循环工作正常,购买它们太慢了。 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

It is done using lambda and apply :使用lambda完成并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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM