Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:合并部分重叠的数据帧_Python_Pandas - Fatal编程技术网

Python:合并部分重叠的数据帧

Python:合并部分重叠的数据帧,python,pandas,Python,Pandas,我有一个数据帧df1,比如: col1、col2等的值是浮点数,>=0。 name的值是字符串,其中每个名称唯一地标识每一行 组的值是字符串。本栏描述了一组名称,为完整起见,将其包括在内。 另一个数据帧df2,如: df1和df2之间没有通用名称值。 df2.group还包括值G1或G2 df2的列可以是df1的一部分,如col2、col4和col5,也可以是df2独有的列,如col7。 我希望这样合并这两个数据帧: name | group | col1 | col2 | col3 | col

我有一个数据帧df1,比如:

col1、col2等的值是浮点数,>=0。 name的值是字符串,其中每个名称唯一地标识每一行 组的值是字符串。本栏描述了一组名称,为完整起见,将其包括在内。 另一个数据帧df2,如:

df1和df2之间没有通用名称值。 df2.group还包括值G1或G2 df2的列可以是df1的一部分,如col2、col4和col5,也可以是df2独有的列,如col7。 我希望这样合并这两个数据帧:

name | group | col1 | col2 | col3 | col4 | col5 | col7
id1  | G1    |                                  |  0
id2  | G1    |                                  |  0
id3  | G1    |                                  |  0
id4  | G2    |                                  |  0
...
id10 | G2    |  0   |      |  0   |      |      |
id11 | G1    |  0   |      |  0   |      |      |
id12 | G1    |  0   |      |  0   |      |      |
...
id20
将df2的行追加到df1,并获取它们的列的集合并集。 如果原始数据帧中的行在新列下没有值,则合并数据帧中的值将为零。e、 g.df1中没有col7,因此在合并的数据帧中,来自df1的所有行将在col7下获得值0。对于源自df2的所有行以及列col1和col3都是相同的,它们是df1所特有的。
结果比我想象的要容易得多:

df_union_all= pd.concat([df1, df2])

那么你的问题是什么?
name | group | col1 | col2 | col3 | col4 | col5 | col7
id1  | G1    |                                  |  0
id2  | G1    |                                  |  0
id3  | G1    |                                  |  0
id4  | G2    |                                  |  0
...
id10 | G2    |  0   |      |  0   |      |      |
id11 | G1    |  0   |      |  0   |      |      |
id12 | G1    |  0   |      |  0   |      |      |
...
id20
df_union_all= pd.concat([df1, df2])