📌  相关文章
📜  如何在索引上合并两个 Pandas DataFrame?

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

如何在索引上合并两个 Pandas DataFrame?

在本文中,我们将讨论如何在 Index 上合并两个 Pandas Dataframes。

方法一:使用join()

该方法用于基于索引加入数据帧。

语法

dataframe1.join(dataframe2)

例子:

Python3
# import pandas module
import pandas as pd
 
# create student  dataframe
data1 = pd.DataFrame({'id': [1, 2, 3, 4],
                      'name': ['manoj', 'manoja', 'manoji', 'manij']},
                     index=['one', 'two', 'three', 'four'])
 
 
# create marks  dataframe
data2 = pd.DataFrame({'s_id': [1, 2, 3, 6, 7],
                      'marks': [98, 90, 78, 86, 78]},
                     index=['one', 'two', 'three', 'siz', 'seven'])
 
# join two dataframes
print(data1.join(data2))


Python3
# import pandas module
import pandas as pd
 
# create student  dataframe
data1 = pd.DataFrame({'id': [1, 2, 3, 4],
                      'name': ['manoj', 'manoja', 'manoji', 'manij']},
                     index=['one', 'two', 'three', 'four'])
 
 
# create marks  dataframe
data2 = pd.DataFrame({'s_id': [1, 2, 3, 6, 7],
                      'marks': [98, 90, 78, 86, 78]},
                     index=['one', 'two', 'three', 'siz', 'seven'])
 
# join two dataframes with merge
print(pd.merge(data1, data2, left_index=True, right_index=True))


Python3
# import pandas module
import pandas as pd
 
# create student  dataframe
data1 = pd.DataFrame({'id': [1, 2, 3, 4],
                      'name': ['manoj', 'manoja', 'manoji', 'manij']},
                     index=['one', 'two', 'three', 'four'])
 
 
# create marks  dataframe
data2 = pd.DataFrame({'s_id': [1, 2, 3, 6, 7],
                      'marks': [98, 90, 78, 86, 78]},
                     index=['one', 'two', 'three', 'siz', 'seven'])
 
# join two dataframes with concat
print(pd.concat([data1, data2], axis=1))


输出:

方法2:使用merge()

这将合并两个具有匹配索引的数据帧

语法

pandas.merge(dataframe1, dataframe2, left_index=True, right_index=True)

在哪里,

  • dataframe1 是第一个数据帧
  • dataframe2 是第二个数据帧
  • left_index 指定第一个数据帧索引设置为真
  • right_index 指定第二个数据帧索引设置为真

例子:

Python3

# import pandas module
import pandas as pd
 
# create student  dataframe
data1 = pd.DataFrame({'id': [1, 2, 3, 4],
                      'name': ['manoj', 'manoja', 'manoji', 'manij']},
                     index=['one', 'two', 'three', 'four'])
 
 
# create marks  dataframe
data2 = pd.DataFrame({'s_id': [1, 2, 3, 6, 7],
                      'marks': [98, 90, 78, 86, 78]},
                     index=['one', 'two', 'three', 'siz', 'seven'])
 
# join two dataframes with merge
print(pd.merge(data1, data2, left_index=True, right_index=True))

输出:

方法 3:使用 concat()

我们可以通过设置axis = 1使用concat()方法连接两个数据帧。

语法

pandas.concat([daatframe1,dataframe2], axis=1)

例子:

Python3

# import pandas module
import pandas as pd
 
# create student  dataframe
data1 = pd.DataFrame({'id': [1, 2, 3, 4],
                      'name': ['manoj', 'manoja', 'manoji', 'manij']},
                     index=['one', 'two', 'three', 'four'])
 
 
# create marks  dataframe
data2 = pd.DataFrame({'s_id': [1, 2, 3, 6, 7],
                      'marks': [98, 90, 78, 86, 78]},
                     index=['one', 'two', 'three', 'siz', 'seven'])
 
# join two dataframes with concat
print(pd.concat([data1, data2], axis=1))

输出: