Python 连接两个数据帧,并将重复的数据帧放入
对于仅具有2019年数据的df2:Python 连接两个数据帧,并将重复的数据帧放入,python,pandas,Python,Pandas,对于仅具有2019年数据的df2: type year value 0 a 2019 13 1 b 2019 5 2 c 2019 5 3 d 2019 20 df1具有多年数据: type year value 0 a 2015 12 1 a 2016 2 2 a 2019 3 3 b 2018 50 4 b 2019 10
type year value
0 a 2019 13
1 b 2019 5
2 c 2019 5
3 d 2019 20
df1
具有多年数据:
type year value
0 a 2015 12
1 a 2016 2
2 a 2019 3
3 b 2018 50
4 b 2019 10
5 c 2017 1
6 c 2016 5
7 c 2019 8
我需要将它们连接在一起,同时用df1
当年的值替换2019
中的df2
值
预期结果如下:
type date value
0 a 2015 12
1 a 2016 2
2 b 2018 50
3 c 2017 1
4 c 2016 5
5 a 2019 13
6 b 2019 5
7 c 2019 5
8 d 2019 20
来自pd.concat([df1,df2],ignore_index=True,sort=False)
的结果,对于一个类型
,在2019年的年显然有多个值。我应该如何改进代码?多谢各位
type date value
0 a 2019 13
1 b 2019 5
2 c 2019 5
3 d 2019 20
4 a 2015 12
5 a 2016 2
6 a 2019 3
7 b 2018 50
8 b 2019 10
9 c 2017 1
10 c 2016 5
11 c 2019 8
在concat
之后,根据类型和日期添加获取最后一行
如果类型
和日期
对在两个数据帧中都是唯一的,则解决方案有效
df = (pd.concat([df1, df2], ignore_index=True, sort =False)
.drop_duplicates(['type','date'], keep='last'))
keep='last'
是否意味着将采用df1
中的值,而不是df2
?如果我写df=(pd.concat([df2,df1],ignore_index=True,sort=False)。drop_duplicates(['type','date'],keep='last'))
@ahbon-这意味着它得到concanated行的最后一个值,所以这里从df2
如果存在,如果不存在,从df1.point-在pd.concat([df1,df2],ignore_index=True,sort=False)之后df1中的值位于前4行,因此keep='last'保留原始值,不使用新值更新。我需要将它们连接在一起,同时将2019年的df2值替换为df1当年的值。
。。最好是更改名称:)+1可以将df=(pd.concat([df1,df2],ignore_index=True,sort=False)更改为df=(pd.concat([df1,df2[df2.columns.intersection(df1.columns)],忽略_index=True,sort=False)。删除重复项(['type','date','a',b,c',keep='last')))
-