码迷,mamicode.com
首页 > 编程语言 > 详细

Python union,union 2个dataset数据

时间:2020-06-17 18:12:48      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:dataset   dataframe   add   code   ignore   amp   nio   create   info   

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数据

标签:dataset   dataframe   add   code   ignore   amp   nio   create   info   

原文地址:https://www.cnblogs.com/watermarks/p/13153910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!