📜  如何堆叠多个 Pandas DataFrame?

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

如何堆叠多个 Pandas DataFrame?

在本文中,我们将了解如何堆叠多个 pandas 数据帧。堆叠意味着将数据帧行附加到第二个数据帧等等。如果有 4 个数据帧,则堆叠后的结果将是一个数据帧,顺序为 dataframe1,dataframe2,dataframe3,dataframe4

方法一:使用 concat() 方法

此方法将按照它们给出的顺序堆叠 pandas 数据帧的行。

注意:如果 ignore_index 参数未设置为 true 意味着它将采用给定的索引,这会导致数据帧的错误堆叠

示例 1 :垂直堆叠两个数据帧的Python程序

Python3
# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby',
                               'ojaswi', 'rohith', 
                               'gnanesh'],
                      'subjects': ['java', 'python',
                                   'php', 'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'],
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# stack the two DataFrames
pd.concat([data1, data2], ignore_index=True, axis=0)


Python3
# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',
                               'rohith', 'gnanesh'],
                      'subjects': ['java', 'python', 'php',
                                   'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'], 
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames
pd.concat([data1, data2, data3, data4], ignore_index=True,axis=0)


Python3
# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi', 
                               'rohith', 'gnanesh'], 
                      'subjects': ['java', 'python',
                                   'php', 'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'], 
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames horizontally
pd.concat([data1, data2, data3, data4], axis=1, ignore_index=True)


Python3
# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',
                               'rohith', 'gnanesh'],
                      'subjects': ['java', 'python', 'php',
                                   'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'],
                      'subjects': [ 'c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames using append()
data1.append([data2, data3, data4], ignore_index=True)


输出

示例 2:垂直堆叠多个数据帧的Python代码

Python3

# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',
                               'rohith', 'gnanesh'],
                      'subjects': ['java', 'python', 'php',
                                   'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'], 
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames
pd.concat([data1, data2, data3, data4], ignore_index=True,axis=0)

输出

示例 3:水平堆叠多个数据帧的Python程序

Python3

# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi', 
                               'rohith', 'gnanesh'], 
                      'subjects': ['java', 'python',
                                   'php', 'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'], 
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames horizontally
pd.concat([data1, data2, data3, data4], axis=1, ignore_index=True)

输出:

方法二:使用 append() 方法

append() 方法用于在给定数据帧之后附加数据帧。

示例:使用 append() 方法堆叠多个数据帧的Python程序

Python3

# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',
                               'rohith', 'gnanesh'],
                      'subjects': ['java', 'python', 'php',
                                   'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'],
                      'subjects': [ 'c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames using append()
data1.append([data2, data3, data4], ignore_index=True)

输出