📜  将两个 Pandas 系列组合成一个 DataFrame

📅  最后修改于: 2022-05-13 01:55:31.558000             🧑  作者: Mango

将两个 Pandas 系列组合成一个 DataFrame

在这篇文章中,我们将学习如何将两个系列组合成一个 DataFrame?在开始之前让我们看看什么是系列?
Pandas Series是一个可以保存任何数据类型的一维标签数组。换句话说,熊猫系列不过是 Excel 表中的一列。

在 pandas 中有几种连接两个系列的方法。以下是一些方法:

方法 1:使用pandas.concat()

此方法完成了沿轴执行连接操作的所有繁重工作,同时在其他轴上执行索引(如果有)的可选设置逻辑(联合或交集)。

代码:

python
# import pandas library
import pandas as pd
  
# this user defines function
# creates a series
# from the passed list.
def createSeries (series_list):
    
  # create a series
  series_list = pd.Series(series_list)
    
  return series_list
  
# create a series of students
students = createSeries(['ABC', 'DEF',
                         'GHI', 'JKL',
                         'MNO', 'PQR'])  
# create a series of subjects
subject = createSeries(['C++', 'C#', 
                        'RUBY', 'SWIFT',
                        'GO', 'PYTHON'])
# create a series of marks
marks = createSeries([90, 30, 
                      50, 70, 
                      80, 60])
# create a dictonary
data = {"students": students,
        "subject": subject,
        "marks": marks}
  
# Concatenating the series side
# by side as depicted by axis=1
# If you want to concatenate the 
# series one below the other
# change the axis to zero.
df = pd.concat(data,
               axis = 1)
  
# show the dataframe
df


Python3
# import pandas library
import pandas as pd
  
# create a series
a = pd.Series(["ABC", "DEF", 
               "GHI"])
  
# create a series
b = pd.Series(["JKL", "MNO", 
               "PQR"])
  
# combine two series then
# create a dataframe
df = pd.DataFrame(a.append(b, 
                  ignore_index = True))
# show the dataframe
df


Python3
# import pandas library
import pandas as pd
  
# create a series
a = pd.Series(["C++", "JAVA", 
               "PYTHON", "DBMS",
               "C#"], name = "subjects")
  
# create a series
b = pd.Series(["30", "60", 
               "90", "56", 
               "50"], name = "marks")
  
# merge both series 
df = pd.merge(a, b, right_index = True,
               left_index = True)
# show the dataframe
df


Python3
# import pandas library
import pandas as pd
  
# create a series
a = pd.Series(["C++", "JAVA", 
               "PYTHON", "DBMS", 
               "C#"], name = "subjects")
  
# create a series
b = pd.Series(["30", "60", 
               "90", "56", 
               "50"], name = "marks")
  
# create a dataframe
a = pd.DataFrame(a)
  
# add series 'b' 
# into dataframe 'a'
df = a.join(b)
  
# show the dataframe
df


输出:

使用 concat() 加入三个系列

方法 2:使用Series.append()

此方法是 concat 的快捷方式。此方法沿axis=0 即行连接。 Series.append() 可以连接多个对象。

代码:

Python3

# import pandas library
import pandas as pd
  
# create a series
a = pd.Series(["ABC", "DEF", 
               "GHI"])
  
# create a series
b = pd.Series(["JKL", "MNO", 
               "PQR"])
  
# combine two series then
# create a dataframe
df = pd.DataFrame(a.append(b, 
                  ignore_index = True))
# show the dataframe
df

输出:

使用附加方法将两个系列逐行加入

方法 3:使用pandas.merge()

Pandas 具有高性能的内存连接操作,与 SQL 等 RDBMS 非常相似。合并可用于数据框或命名系列对象之间的所有数据库连接操作。在这种情况下,您必须向系列传递一个额外的参数“名称”。

代码:

Python3

# import pandas library
import pandas as pd
  
# create a series
a = pd.Series(["C++", "JAVA", 
               "PYTHON", "DBMS",
               "C#"], name = "subjects")
  
# create a series
b = pd.Series(["30", "60", 
               "90", "56", 
               "50"], name = "marks")
  
# merge both series 
df = pd.merge(a, b, right_index = True,
               left_index = True)
# show the dataframe
df

输出:

使用合并方法加入两个系列

方法四:使用 Dataframe.join()

此方法也可用于连接两个系列,但您必须将一个系列转换为数据框。

代码:

Python3

# import pandas library
import pandas as pd
  
# create a series
a = pd.Series(["C++", "JAVA", 
               "PYTHON", "DBMS", 
               "C#"], name = "subjects")
  
# create a series
b = pd.Series(["30", "60", 
               "90", "56", 
               "50"], name = "marks")
  
# create a dataframe
a = pd.DataFrame(a)
  
# add series 'b' 
# into dataframe 'a'
df = a.join(b)
  
# show the dataframe
df

输出:

使用 join 方法连接两个系列