📜  如何在Python中使用 cbind?

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

如何在Python中使用 cbind?

在本文中,我们将讨论Python中的 cbind 。我们已经看到 R 编程语言中的 cbind()函数按列组合指定的向量、矩阵或数据帧。但是在Python中,有一个 concat()函数,它相当于 R 的 cbind()函数。

创建数据框进行演示:

Python3
# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]})
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]})
  
# display
print(data1)
  
print(data2)


Python3
# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]})
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]})
  
  
# concat dataframes
pd.concat([data1, data2], axis=1)


Python3
# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]}, index=[0, 1, 2])
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]}, index=[3, 4, 5])
  
  
# concat dataframes
pd.concat([data1, data2], axis=1)


Python3
# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]}, index=[0, 1, 2])
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]}, index=[3, 4, 5])
  
# remove dataframe 1 indices
data1.reset_index(drop=True, inplace=True)
  
# remove dataframe 2 indices
data2.reset_index(drop=True, inplace=True)
  
# concat dataframes
pd.concat([data1, data2], axis=1)


输出

示例 1:具有相同索引的 Concat 数据帧

这将基于索引连接逐行数据。这里两个数据帧索引是相同的。

Python3

# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]})
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]})
  
  
# concat dataframes
pd.concat([data1, data2], axis=1)

输出

示例 2:具有不等索引的 Concat 数据帧

在这种情况下,两个数据帧索引不相等,当我们应用 concat()函数时,这将导致一个具有 NaN 值的新数据帧

Python3

# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]}, index=[0, 1, 2])
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]}, index=[3, 4, 5])
  
  
# concat dataframes
pd.concat([data1, data2], axis=1)

输出

为了删除这些 NaN 行,我们必须使用 reset_index() 方法删除索引

例子:

Python3

# import pandas module
import pandas as pd
  
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject1': ['python', 'R', 'php'],
                      'marks': [96, 89, 90]}, index=[0, 1, 2])
  
  
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
                      'subject2': ['html', '.net', 'jsp'],
                      'marks': [89, 79, 80]}, index=[3, 4, 5])
  
# remove dataframe 1 indices
data1.reset_index(drop=True, inplace=True)
  
# remove dataframe 2 indices
data2.reset_index(drop=True, inplace=True)
  
# concat dataframes
pd.concat([data1, data2], axis=1)

输出