[英]How to concatenate two dataframes without duplicates?
I'd like to concatenate two dataframes A
, B
to a new one without duplicate rows (if rows in B
already exist in A
, don't add):我想将两个数据帧A
, B
连接到一个没有重复行的新数据帧(如果B
中的行已经存在于A
中,请不要添加):
Dataframe A
: Dataframe A
:
I II
0 1 2
1 3 1
Dataframe B
: Dataframe B
:
I II
0 5 6
1 3 1
New Dataframe:新 Dataframe:
I II
0 1 2
1 3 1
2 5 6
How can I do this?我怎样才能做到这一点?
The simplest way is to just do the concatenation, and then drop duplicates.最简单的方法是只进行连接,然后删除重复项。
>>> df1
A B
0 1 2
1 3 1
>>> df2
A B
0 5 6
1 3 1
>>> pandas.concat([df1,df2]).drop_duplicates().reset_index(drop=True)
A B
0 1 2
1 3 1
2 5 6
The reset_index(drop=True)
is to fix up the index after the concat()
and drop_duplicates()
. reset_index(drop=True)
是修复concat()
和drop_duplicates()
之后的索引。 Without it you will have an index of [0,1,0]
instead of [0,1,2]
.没有它,您将拥有[0,1,0]
而不是[0,1,2]
的索引。 This could cause problems for further operations on this dataframe
down the road if it isn't reset right away.如果不立即重置,这可能会导致此dataframe
进一步操作出现问题。
In case you have a duplicate row already in DataFrame A, then concatenating and then dropping duplicate rows, will remove rows from DataFrame A that you might want to keep.如果您在 DataFrame A 中已经有一个重复的行,然后连接并删除重复的行,将从 DataFrame A 中删除您可能想要保留的行。
In this case, you will need to create a new column with a cumulative count, and then drop duplicates, it all depends on your use case, but this is common in time-series data在这种情况下,您需要创建一个具有累积计数的新列,然后删除重复项,这完全取决于您的用例,但这在时间序列数据中很常见
Here is an example:下面是一个例子:
df_1 = pd.DataFrame([
{'date':'11/20/2015', 'id':4, 'value':24},
{'date':'11/20/2015', 'id':4, 'value':24},
{'date':'11/20/2015', 'id':6, 'value':34},])
df_2 = pd.DataFrame([
{'date':'11/20/2015', 'id':4, 'value':24},
{'date':'11/20/2015', 'id':6, 'value':14},
])
df_1['count'] = df_1.groupby(['date','id','value']).cumcount()
df_2['count'] = df_2.groupby(['date','id','value']).cumcount()
df_tot = pd.concat([df_1,df_2], ignore_index=False)
df_tot = df_tot.drop_duplicates()
df_tot = df_tot.drop(['count'], axis=1)
>>> df_tot
date id value
0 11/20/2015 4 24
1 11/20/2015 4 24
2 11/20/2015 6 34
1 11/20/2015 6 14
I'm surprised that pandas doesn't offer a native solution for this task.我很惊讶 Pandas 没有为此任务提供本地解决方案。 I don't think that it's efficient to just drop the duplicates if you work with large datasets (as Rian G suggested).如果您使用大型数据集(如 Rian G 建议的那样),我认为仅删除重复项并不有效。
It is probably most efficient to use sets to find the non-overlapping indices.使用集合来查找非重叠索引可能是最有效的。 Then use list comprehension to translate from index to 'row location' (boolean), which you need to access rows using iloc[,].然后使用列表理解将索引转换为“行位置”(布尔值),您需要使用 iloc[,] 访问行。 Below you find a function that performs the task.下面是一个执行任务的函数。 If you don't choose a specific column (col) to check for duplicates, then indexes will be used, as you requested.如果您不选择特定列 (col) 来检查重复项,则将根据您的要求使用索引。 If you chose a specific column, be aware that existing duplicate entries in 'a' will remain in the result.如果您选择了特定列,请注意“a”中现有的重复条目将保留在结果中。
import pandas as pd
def append_non_duplicates(a, b, col=None):
if ((a is not None and type(a) is not pd.core.frame.DataFrame) or (b is not None and type(b) is not pd.core.frame.DataFrame)):
raise ValueError('a and b must be of type pandas.core.frame.DataFrame.')
if (a is None):
return(b)
if (b is None):
return(a)
if(col is not None):
aind = a.iloc[:,col].values
bind = b.iloc[:,col].values
else:
aind = a.index.values
bind = b.index.values
take_rows = list(set(bind)-set(aind))
take_rows = [i in take_rows for i in bind]
return(a.append( b.iloc[take_rows,:] ))
# Usage
a = pd.DataFrame([[1,2,3],[1,5,6],[1,12,13]], index=[1000,2000,5000])
b = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]], index=[1000,2000,3000])
append_non_duplicates(a,b)
# 0 1 2
# 1000 1 2 3 <- from a
# 2000 1 5 6 <- from a
# 5000 1 12 13 <- from a
# 3000 7 8 9 <- from b
append_non_duplicates(a,b,0)
# 0 1 2
# 1000 1 2 3 <- from a
# 2000 1 5 6 <- from a
# 5000 1 12 13 <- from a
# 2000 4 5 6 <- from b
# 3000 7 8 9 <- from b
Another option:另外一个选择:
concatenation = pd.concat([
dfA,
dfB[dfB['I'].isin(dfA['I']) == False], # <-- get all the data in dfB that doesn't show up in dfB (based on values in column 'I')
])
The object concatenation
will be: object concatenation
将是:
I II
0 1 2
1 3 1
2 5 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.