Python union,union 2个dataset数据

Posted watermarks

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python union,union 2个dataset数据相关的知识,希望对你有一定的参考价值。

Union and union all in Pandas dataframe Python:

Union all of two data frames in pandas can be easily achieved by using concat() function. Lets see with an example. First lets create two data frames

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pandas as pd
import numpy as np
 
#Create a DataFrame
df1 = {
    ‘Subject‘:[‘semester1‘,‘semester2‘,‘semester3‘,‘semester4‘,‘semester1‘,
               ‘semester2‘,‘semester3‘],
   ‘Score‘:[62,47,55,74,31,77,85]}
 
df2 = {
    ‘Subject‘:[‘semester1‘,‘semester2‘,‘semester3‘,‘semester4‘],
   ‘Score‘:[90,47,85,74]}
 
 
df1 = pd.DataFrame(df1,columns=[‘Subject‘,‘Score‘])
df2 = pd.DataFrame(df2,columns=[‘Subject‘,‘Score‘])
 
df1
df2

df1 will be

技术图片

df2 will be

技术图片

 

Union all of dataframes in pandas:

技术图片

 

 

UNION ALL

concat() function in pandas creates the union of two dataframe.

1
2
3
""" Union all in pandas"""
df_union_all= pd.concat([df1, df2])
df_union_all

union all of two dataframes  df1 and df2 is created with duplicates. So the resultant dataframe will be

技术图片

 

Union all of dataframes in pandas and reindex :

concat() function in pandas creates the union of two dataframe with ignore_index = True will reindex the dataframe

1
2
3
""" Union all with reindex in pandas"""
df_union_all= pd.concat([df1, df2],ignore_index=True)
df_union_all

union all of two dataframes  df1 and df2 is created with duplicates and the index is changed. So the resultant dataframe will be

技术图片

 

 

Union of dataframes in pandas:

技术图片

 

 

                        UNION 

 

ref:http://www.datasciencemadesimple.com/union-and-union-all-in-pandas-dataframe-in-python-2/

以上是关于Python union,union 2个dataset数据的主要内容,如果未能解决你的问题,请参考以下文章

#include <stdio.h> main() union char a[2];int k; data; data.a[0]='5'; data.a[1]='

Python set Union 和 set Intersection 的操作方式不同?

Python3x: set中union和update的区别

Union联合体

Spring Data + View with Union 返回重复行

是否可以使用 MagicalRecord/Core Data 进行类似 UNION 的查询