简体   繁体   English

如何使用不同的 datetimeindex 将 pandas dataframe 中的值填充到另一个 dataframe

[英]How to fill values from a pandas dataframe to another dataframe with different datetimeindex

I have two dataframes.我有两个数据框。 One has a 5 minute granularity (df1), the other is indexed by days (df2).一个有 5 分钟的粒度 (df1),另一个按天索引 (df2)。 For the sake of this example the days end at 7:10在本例中,日期在 7:10 结束

df1: df1:

            Date    Close
2019-06-20 07:00:00 2927.25
2019-06-20 07:05:00 2927.00
2019-06-20 07:10:00 2926.75
2019-06-21 07:00:00 2932.25
2019-06-21 07:05:00 2932.25
2019-06-21 07:10:00 2931.00
2019-06-24 07:00:00 2941.75
2019-06-24 07:05:00 2942.25
2019-06-24 07:10:00 2941.50
2019-06-25 07:00:00 2925.50
2019-06-25 07:05:00 2926.50
2019-06-25 07:10:00 2926.50

df2: df2:

            range                       
Date                            
2019-06-20  115.0
2019-06-21  86.0    
2019-06-24  52.0
2019-06-25  132.0   

Now I'd like to take the values from 'range' column of df2 and and inject them repetitive in a new column in df1.现在我想从 df2 的“范围”列中获取值,并将它们重复地注入到 df1 的新列中。

It should look like this:它应该如下所示:

            Date    Close       range
2019-06-20 07:00:00 2927.25     115.0
2019-06-20 07:05:00 2927.00     115.0
2019-06-20 07:10:00 2926.75     115.0
2019-06-21 07:00:00 2932.25     86.0    
2019-06-21 07:05:00 2932.25     86.0    
2019-06-21 07:10:00 2931.00     86.0    
2019-06-24 07:00:00 2941.75     52.0
2019-06-24 07:05:00 2942.25     52.0
2019-06-24 07:10:00 2941.50     52.0
2019-06-25 07:00:00 2925.50     132.0
2019-06-25 07:05:00 2926.50     132.0
2019-06-25 07:10:00 2926.50     132.0

Unfortunately I don't know how to start that's why there's no 'my attempt' code How would you do this?不幸的是我不知道如何开始这就是为什么没有“我的尝试”代码你会怎么做?

First convert the date like columns to pandas datetime series:首先将日期类列转换为 pandas 日期时间序列:

df1['Date'] = pd.to_datetime(df1['Date'])
df2.index = pd.to_datetime(df2.index)

Use Series.dt.date + Series.map to map range values from df2 to df1 :使用Series.dt.date + Series.map到 map range值从df2df1

df1['range'] = df1['Date'].dt.date.map(df2.set_index(df2.index.date)['range'])

OR its also possible to use DataFrame.merge :或者它也可以使用DataFrame.merge

df1.assign(k=df1['Date'].dt.date).merge(df2.assign(k=df2.index.date), on='k').drop('k', 1)

Result:结果:

                  Date    Close  range
0  2019-06-20 07:00:00  2927.25  115.0
1  2019-06-20 07:05:00  2927.00  115.0
2  2019-06-20 07:10:00  2926.75  115.0
3  2019-06-21 07:00:00  2932.25   86.0
4  2019-06-21 07:05:00  2932.25   86.0
5  2019-06-21 07:10:00  2931.00   86.0
6  2019-06-24 07:00:00  2941.75   52.0
7  2019-06-24 07:05:00  2942.25   52.0
8  2019-06-24 07:10:00  2941.50   52.0
9  2019-06-25 07:00:00  2925.50  132.0
10 2019-06-25 07:05:00  2926.50  132.0
11 2019-06-25 07:10:00  2926.50  132.0

If you want to make a loop, do something like that:如果你想制作一个循环,请执行以下操作:

for i in df2["Date"]:
     for j in df1["Date"]:
          if i==j:
              df1['range'] = df2['range']
  1. need to ensure your Date columns are dates需要确保您的日期列是日期
  2. floor them to day granularity so they are same across data frames将它们设置为日粒度,因此它们在数据帧中是相同的
  3. cleanup the columns清理列
df1 = pd.DataFrame({"Date":["2019-06-19T23:00:00.000Z","2019-06-19T23:05:00.000Z","2019-06-19T23:10:00.000Z","2019-06-20T23:00:00.000Z","2019-06-20T23:05:00.000Z","2019-06-20T23:10:00.000Z","2019-06-23T23:00:00.000Z","2019-06-23T23:05:00.000Z","2019-06-23T23:10:00.000Z","2019-06-24T23:00:00.000Z","2019-06-24T23:05:00.000Z","2019-06-24T23:10:00.000Z"],"Close":[2927.25,2927,2926.75,2932.25,2932.25,2931,2941.75,2942.25,2941.5,2925.5,2926.5,2926.5]})
df2 = pd.DataFrame({"Date":["2019-06-19T16:00:00.000Z","2019-06-20T16:00:00.000Z","2019-06-23T16:00:00.000Z","2019-06-24T16:00:00.000Z"],"range":[115,86,52,132]})

df1.Date = pd.to_datetime(df1.Date)
df2.Date = pd.to_datetime(df2.Date)


df1.assign(day=df1.Date.dt.floor("D"))\
    .merge(df2.assign(day=df2.Date.dt.floor("D")), on="day")\
    .drop(["day","Date_y"],1).rename({"Date_x":"Date"},axis=1)

output output

                     Date    Close  range
2019-06-19 23:00:00+00:00  2927.25    115
2019-06-19 23:05:00+00:00  2927.00    115
2019-06-19 23:10:00+00:00  2926.75    115
2019-06-20 23:00:00+00:00  2932.25     86
2019-06-20 23:05:00+00:00  2932.25     86
2019-06-20 23:10:00+00:00  2931.00     86
2019-06-23 23:00:00+00:00  2941.75     52
2019-06-23 23:05:00+00:00  2942.25     52
2019-06-23 23:10:00+00:00  2941.50     52
2019-06-24 23:00:00+00:00  2925.50    132
2019-06-24 23:05:00+00:00  2926.50    132
2019-06-24 23:10:00+00:00  2926.50    132

暂无
暂无

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

相关问题 用其他形状填充另一个Pandas数据框中的缺失值 - Fill missing values from another Pandas dataframe with different shape 如何根据另外两个数据帧的值填充 Pandas 数据帧 - How to fill the Pandas Dataframe based on values from another two dataframes 熊猫用另一个数据框范围内的值计数填充数据框 - Pandas fill dataframe with count of values within a range from another dataframe Pandas 从另一个 dataframe 填充 dataframe 中的缺失值 - Pandas fill missing values in dataframe from another dataframe 熊猫从另一个数据帧填充一个数据帧上的空值 - Pandas fill empty values on one dataframe from another dataframe Pandas:重新采样 dataframe 以匹配不同 dataframe 的 DatetimeIndex - Pandas: resample a dataframe to match a DatetimeIndex of a different dataframe 如何使用 Pandas 中的另一个 DataFrame 填充 DataFrame 中的缺失值 - How to fill missing values in DataFrame using another DataFrame in Pandas 如何通过匹配来自另一个数据帧熊猫的值来填充数据帧中的列的值 - How to fill in values for a column in a dataframe by matching values from another dataframe pandas 如何使用 Pandas 使用来自第二个数据帧但依赖于当前数据帧中不同现有列的值填充新列 - How To Fill New Column With Values From Second Dataframe but Dependent on Different Existing Column in Current Dataframe using Pandas 从 Pandas DataFrame 中的 DateTimeIndex 获取整数索引值 - Get integer index values from DateTimeIndex in pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM