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 of dataframes in pandas 2

 

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 of dataframes in pandas 3

 

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 and union all in pandas 14

 

 

Union of dataframes in pandas:

 

 

                        UNION 

 

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

posted @   po-A  阅读(2581)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~

Tushare大数据开放社区-免费提供各类金融数据和区块链数据 , 助力智能投资与创新型投资。

Python, Matlab, R, Web数据抽取学习交流。

点击右上角即可分享
微信分享提示