[英]How to merge two different size DataFrames in Pandas to update one dataframe depends on matching partial values in one column with another dataframe
I am trying to code a task for work, so I made a simple case that simulates this task.我正在尝试为工作编写任务代码,因此我制作了一个模拟此任务的简单案例。 I have two dataframes:
data_1
and data_2
, and I would like to replace some rows in data_1
with rows from data_2
by condition of matching values in a column named time
.我有两个数据帧:
data_1
和data_2
,我想通过匹配名为time
的列中的值的条件,用data_1
中的行替换data_2
中的一些行。
Here is an example:这是一个例子:
import numpy as np
import pandas as pd
a = {
'time':[1,2,3,4,5,6],
'column_1':[2,2,2,2,2,2],
'column_2':[3,3,3,3,3,3]
}
b = {
'time':[3,4,5],
'column_1':[0,0,0],
'column_2':[0,0,0]
}
data_1 = pd.DataFrame(a)
data_2 = pd.DataFrame(b)
In the result, I would like to get dataframe like this:结果,我想这样得到 dataframe:
time column_1 column_2
0 1 2 3
1 2 2 3
2 3 0 0
3 4 0 0
4 5 0 0
5 6 2 3
I tried merge and replace methods in Pandas, but it was not successful.我尝试了Pandas中的merge和replace方法,但是没有成功。 I did mask boolean array:
我做了掩码 boolean 阵列:
time_1 = list(data_1['time'])
time_2 = list(data_2['time'])
mask_array = np.zeros(len(time_1),dtype = bool)
for i, item in enumerate(time_1):
if item in time_2:
mask_array[i] = True
and I received:我收到了:
array([False, False, True, True, True, False])
But I could not replace data_1
values with data_2
values.但我无法用
data_1
值替换data_2
值。 What did I do wrong?我做错什么了? It does not seem like a difficult task, but I could not find anything useful and just do not know what to do.
这似乎不是一项艰巨的任务,但我找不到任何有用的东西,只是不知道该怎么做。 I do not have a lot of experience with pandas, so maybe I do not understand something.
我对 pandas 没有太多经验,所以也许我不明白某些事情。
You can use .update()
after setting index on time
on both data_1a
and data_1b
, as follows:您可以在
data_1a
和data_1b
上按time
设置索引后使用.update()
,如下所示:
data_1a = data_1.set_index('time')
data_1a.update(data_2.set_index('time'))
data_out = data_1a.reset_index()
.update()
modifies in place using non-NA values from another DataFrame. Aligns on indices. .update()
使用来自另一个 DataFrame 的非 NA 值就地修改。对齐索引。 Thus, when you set time
as index on both data_1a
and data_1b
, .update()
aligns on matching values in column time
to perform the update of data_1
by corresponding values of data_2
.因此,当您将
time
设置为data_1a
和data_1b
的索引时, .update()
对齐列time
中的匹配值,以通过data_1
的相应值执行data_2
的更新。
Data Setup:数据设置:
a = {
'time':[1,2,3,4,5,6],
'column_1':[2,2,2,2,2,2],
'column_2':[3,3,3,3,3,3]
}
b = {
'time':[3,4,5],
'column_1':[0,0,0],
'column_2':[0,0,0]
}
data_1 = pd.DataFrame(a)
data_2 = pd.DataFrame(b)
Result:结果:
print(data_out)
time column_1 column_2
0 1 2.0 3.0
1 2 2.0 3.0
2 3 0.0 0.0
3 4 0.0 0.0
4 5 0.0 0.0
5 6 2.0 3.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.