简体   繁体   English

合并来自两个不同数据框的列

[英]merging columns from two different data frame

I have two different dataframe as shown in the images below.我有两个不同的数据框,如下图所示。 I will like to merge column (C-ORG_PC) in the first dataframe with column (C-ORG_PC) in the second dataframe such that when the SAMPFROM column in both dataframe are equal, It will replace the -99 value at that point in the first dataframe with the corresponding value in the second dataframe.我想将第一个数据帧中的列 (C-ORG_PC) 与第二个数据帧中的列 (C-ORG_PC) 合并,这样当两个数据帧中的 SAMPFROM 列相等时,它将替换该点的 -99 值第一个数据帧与第二个数据帧中的相应值。

data1:数据1:

SAMPFROM采样器 SAMPTO桑普托 C-ORG组织结构
0 0 5 5 -99 -99
5 5 10 10 -99 -99
10 10 15 15 -99 -99
15 15 20 20 -99 -99
20 20 25 25 -99 -99
25 25 30 30 -99 -99
30 30 35 35 -99 -99
35 35 40 40 -99 -99
40 40 45 45 -99 -99

data2:数据2:

SAMPFROM采样器 SAMPTO桑普托 C-ORG组织结构
0 0 20 20 2.5 2.5
20 20 40 40 7 7
40 40 60 60 7 7
60 60 80 80 9 9
 LIST1=list(data1['SAMPFROM'])
LIST2=list(data2['SAMPFROM'])
values=[]
for i in LIST1:
 for j in LIST2:
    if i==j:
        values.append(i)
for k in values:
 for q in data2['SAMPFROM']:
    if k==q:
        data1['new']=data2['CO3_pct_BESTEL']

You can use the code below:您可以使用以下代码:

df1 = data1.set_index('SAMPFROM')
df1.update(data2.set_index('SAMPFROM')['C-ORG'])
df1.reset_index()

   SAMPFROM  SAMPTO  C-ORG
0         0       5    2.5
1         5      10  -99.0
2        10      15  -99.0
3        15      20  -99.0
4        20      25    7.0
5        25      30  -99.0
6        30      35  -99.0
7        35      40  -99.0
8        40      45    7.0

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM