如何根据 Python 中的条件将另一个数据帧的值分配给当前数据帧?

How to assign values from another dataframe to current dataframe based on a condition in Python?

我想根据 DatetimeIndex 条件将一个数据帧的值分配给另一个数据帧。

我有这个数据框:(第一个)

date                importance
2006-12-05 10:35:00     HIGH
2006-12-13 02:40:00     LOW

这个数据框:(第二个)

index                     value
2006-12-05 08:03:01.985    6
2006-12-05 08:11:34.130    7
2006-12-05 08:20:05.959    6
2006-12-05 08:28:38.104    6
2006-12-05 08:37:02.995    6
2006-12-05 08:45:35.140    5
2006-12-05 08:54:06.969    6
2006-12-05 09:02:59.928    6
2006-12-05 09:11:32.072    6
2006-12-05 09:20:03.901    6
2006-12-05 09:28:36.046    5
2006-12-05 09:37:00.937    5
2006-12-05 09:45:33.082    6
2006-12-05 09:54:04.911    6
2006-12-05 10:02:04.889    6
2006-12-05 10:10:37.034    5
2006-12-05 10:19:08.863    6
2006-12-05 10:27:41.008    5
2006-12-05 10:36:04.953    5
2006-12-05 10:44:37.098    5
.
.
.
2006-12-13 02:06:00.898    1
2006-12-13 02:14:33.043    1
2006-12-13 02:23:04.872    1
2006-12-13 02:31:03.904    1
2006-12-13 02:39:36.048    1
2006-12-13 02:48:07.878    2
2006-12-13 02:56:40.022    5
2006-12-13 03:05:04.914    2
2006-12-13 03:13:37.058    3
2006-12-13 03:22:08.888    6
2006-12-13 03:31:03.108    1
2006-12-13 03:39:34.937    1
2006-12-13 03:48:07.081    1
2006-12-13 03:56:38.911    2
2006-12-13 04:05:04.117    3

最终结果应该是这样的:

index                      value    new_value
2006-12-05 08:03:01.985    6        
2006-12-05 08:11:34.130    7        
2006-12-05 08:20:05.959    6        
2006-12-05 08:28:38.104    6
2006-12-05 08:37:02.995    6
2006-12-05 08:45:35.140    5
2006-12-05 08:54:06.969    6
2006-12-05 09:02:59.928    6
2006-12-05 09:11:32.072    6
2006-12-05 09:20:03.901    6
2006-12-05 09:28:36.046    5
2006-12-05 09:37:00.937    5
2006-12-05 09:45:33.082    6
2006-12-05 09:54:04.911    6
2006-12-05 10:02:04.889    6
2006-12-05 10:10:37.034    5
2006-12-05 10:19:08.863    6
2006-12-05 10:27:41.008    5        
2006-12-05 10:36:04.953    5            HIGH
2006-12-05 10:44:37.098    5
.
.
.
2006-12-13 02:06:00.898    1
2006-12-13 02:14:33.043    1
2006-12-13 02:23:04.872    1
2006-12-13 02:31:03.904    1
2006-12-13 02:39:36.048    1            LOW
2006-12-13 02:48:07.878    2
2006-12-13 02:56:40.022    5
2006-12-13 03:05:04.914    2
2006-12-13 03:13:37.058    3
2006-12-13 03:22:08.888    6
2006-12-13 03:31:03.108    1
2006-12-13 03:39:34.937    1
2006-12-13 03:48:07.081    1
2006-12-13 03:56:38.911    2
2006-12-13 04:05:04.117    3

我试过这个:

def getNearestDate(items, pivot):
    return min(items, key=lambda x: abs(x - pivot))

items = second_df.index
for pivot in first_df.date:
    d = getNearestDate(items, pivot)
    print(d)
    second_df.loc[second_df.index == d, 'new_value'] = first_df.importance

它打印最近的这些日期:

2006-12-05 10:36:04.953000
2006-12-13 02:39:36.048000

所以在这些日子里,它应该把值放在“重要性”上。 此外,在 new_value 列上,所有内容都是 NAN。

你能帮我解决这个问题吗?

你已经使用了loc中的条件 second_df.index == d 并且它 return 在满足条件的索引处为真,而不是索引。

改为使用 second_df[second_df.index == d].index.values

您已经有了 second_df.index == d 想要的面具。这会产生一个 pandas.Series,其中值为 True,值为真,值为 False,值为假。您可以 |= 多个掩码一起获取任何掩码中 True 的所有行。只需将该系列作为 'new_value' 列附加到您的第二个数据框。

mask = False
for pivot in first_df.date:
    mask |= second_df.index == getNearestDate(second_df.index, pivot)
second_df['new_value'] = mask

如果你真的想让 'X''' 成为 TrueFalse 的别名,你也可以在添加之前应用一个简单的 lambda 来转换它们系列到数据框。

mask = False
for pivot in first_df.date:
    mask |= second_df.index == getNearestDate(second_df.index, pivot)
second_df['new_value'] = mask.apply(lambda x: 'X' if bool(x) else '')

编辑:

如果要获取第一个数据帧的 importance 值,只需使用 getNearestDate 函数确定哪些行需要该值,然后将它们与第二个数据帧合并。

first_df['index'] = first_df.apply(
    lambda x: getNearestDate(second_df.index, x.date),
    axis = 1,
    result_type = 'reduce'
)
second_df = second_df.merge(first_df, how='left', on='index')

您应该可以使用 reindexmerge

# note the method and the tolerance. Change them to whatever works best for your actual data
new_df = df2.merge(df.reindex(df2.index, method='nearest', limit=1, tolerance='2T'),
                   left_index=True, right_index=True)


                         value importance
index                                    
2006-12-05 08:03:01.985      6        NaN
2006-12-05 08:11:34.130      7        NaN
2006-12-05 08:20:05.959      6        NaN
2006-12-05 08:28:38.104      6        NaN
2006-12-05 08:37:02.995      6        NaN
2006-12-05 08:45:35.140      5        NaN
2006-12-05 08:54:06.969      6        NaN
2006-12-05 09:02:59.928      6        NaN
2006-12-05 09:11:32.072      6        NaN
2006-12-05 09:20:03.901      6        NaN
2006-12-05 09:28:36.046      5        NaN
2006-12-05 09:37:00.937      5        NaN
2006-12-05 09:45:33.082      6        NaN
2006-12-05 09:54:04.911      6        NaN
2006-12-05 10:02:04.889      6        NaN
2006-12-05 10:10:37.034      5        NaN
2006-12-05 10:19:08.863      6        NaN
2006-12-05 10:27:41.008      5        NaN
2006-12-05 10:36:04.953      5       HIGH
2006-12-05 10:44:37.098      5        NaN
2006-12-13 02:06:00.898      1        NaN
2006-12-13 02:14:33.043      1        NaN
2006-12-13 02:23:04.872      1        NaN
2006-12-13 02:31:03.904      1        NaN
2006-12-13 02:39:36.048      1        LOW
2006-12-13 02:48:07.878      2        NaN
2006-12-13 02:56:40.022      5        NaN
2006-12-13 03:05:04.914      2        NaN
2006-12-13 03:13:37.058      3        NaN
2006-12-13 03:22:08.888      6        NaN
2006-12-13 03:31:03.108      1        NaN
2006-12-13 03:39:34.937      1        NaN
2006-12-13 03:48:07.081      1        NaN
2006-12-13 03:56:38.911      2        NaN
2006-12-13 04:05:04.117      3        NaN

只需进行这些小改动,希望它会起作用

loc=[]

    def getNearestDate(items, pivot):
        return min(items, key=lambda x: abs(x - pivot))
    
    items = second_df.index
    for pivot in first_df.date:
        d = getNearestDate(items, pivot)
        loc.append(second_df.set_index('index').index.get_loc(d))
    
    ## Adding Data to your second df   
    second_df['importance']=[]
    for index,locations in enumerate(loc):
        df['importance'][int(location)]=first_df['importance'][index]

首先我们必须保留与原始数据框中的日期相对应的日期:

items = second_df.index
dates = []

for pivot in first_df.date:
    dates.append(getNearestDate(items, pivot))
    
first_df['new_date'] = dates

因为我们不再需要它们,我们可以删除整列:

first_df = first_df.drop(columns="date")

为了使合并工作,我们需要在两个数据帧上声明索引。

first_df.set_index("new_date", inplace =True)

合并完成如下:

second_df = second_df.merge(first_df, how = "left",left_index=True, right_index=True)

此外,永远不要让 NaN 出现在数据框中很重要:

second_df.importance = second_df.importance.fillna(0)