Pandas 数据帧align()
函数
原文:https://www.studytonight.com/pandas/pandas-dataframe-align-function
在本教程中,我们将学习 PythonPandasDataFrame.align()
方法。此方法使用指定的连接方法将两个对象在其轴上对齐。当我们希望使用不同的连接方法(如外部、内部、左侧和右侧)将一个数据帧与另一个数据帧或一个数据帧与一个系列同步时,此方法很有帮助。
句法
使用该函数所需的语法如下。
DataFrame.align(other, join='outer', axis=None, level=None, copy=True, fill_value=None, method=None, limit=None, fill_axis=0, broadcast_axis=None)
参数:
其他:可以是数据帧或序列。
连接:‘外’‘内’‘左’‘右’。默认值为“外部”连接。
轴:其他对象允许的轴,默认无。在索引(0)、列(1)或两者上对齐(无)。
级别: int 或级别名,默认无。跨级别广播,匹配传递的多索引级别上的索引值。
副本: bool,默认 True。总是返回新对象。如果 copy=False 并且不需要重新索引,则返回原始对象。
fill_value: 标量,缺省为 np.NaN .用于缺失值的值。默认为 NaN,但可以是任何“兼容”的值。
方法 { '回填','填充','填充','无' },默认无
限制: int,默认无。
fill_axis {0 或' index ',1 或' columns'},默认为 0。填充轴、方法和限制。
broadcast_axis {0 或' index ',1 或' columns'},默认无。如果对齐两个不同尺寸的对象,则沿此轴广播值。
示例:创建和打印数据帧
创建两个具有不同索引、列的数据帧并打印输出。
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
print("----------Printing DataFrame 1----------")
print(df1)
print("----------Printing DataFrame 2----------")
print(df2)
一旦我们运行程序,我们将得到以下结果。
-打印数据帧 1 - 名称卷无主题标记 1 Abhishek 100 Science 90 2 Anurag 101 Science 85 -打印数据帧 2 - 名称卷无标记 2 Chetan 103 75 3 Divya 104 80 4 Diya 105 92
示例:与Columns(axis=1)
上的Left
连接对齐
以下示例显示了如何将两个不同的数据帧与列上的left
连接对齐。
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='left', axis=1)
print("---------After Aligning to the left DataFrame 1--------")
print(a1)
print("---------After Aligning to the left DataFrame 2--------")
print(a2)
一旦我们运行程序,我们将得到以下结果。
-对齐左侧数据帧 1 - 名称卷无主题标记 1 Abhishek 100 Science 90 2 anu rag 101 Science 85 -对齐左侧数据帧 2 - 名称卷无主题标记 2 Chetan 103 NaN 75 3 Divya 104 NaN 80 4 Diya 105 NaN 92
示例:与Columns (axis=1)
上的right
连接对齐
以下示例显示了如何将两个不同的数据帧与列上的right join
对齐。
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='right', axis=1)
print("---------After Aligning to the right DataFrame 1--------")
print(a1)
print("---------After Aligning to the right DataFrame 2--------")
print(a2)
一旦我们运行程序,我们将得到以下结果。
-对准右侧数据帧 1 - 名称滚动无标记 1 Abhishek 100 90 2 Anurag 101 85 -对准右侧数据帧 2 - 名称滚动无标记 2 Chetan 103 75 3 Divya 104 80 4 Diya 105 92
示例:与Columns(axis=1)
上的outer
连接对齐
以下示例显示了如何将两个不同的数据帧与列上的outer
连接对齐。
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='outer', axis=1)
print("---------After Aligning to the outer DataFrame 1--------")
print(a1)
print("---------After Aligning to the outer DataFrame 2--------")
print(a2)
一旦我们运行程序,我们将得到以下结果。
-与外部数据帧 1 对齐后- 标记名称卷无主题 1 90 Abhishek 100 Science 2 85 Anurag 101 Science -与外部数据帧 2 对齐后- 标记名称卷无主题 2 75 Chetan 103 NaN 3 80 Divya 104 NaN 4 92 Diya 105 NaN
示例:与Columns(axis=1)
上的inner
连接对齐
以下示例显示了如何将两个不同的数据帧与列上的left
连接对齐。
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='inner', axis=1)
print("---------After Aligning to the inner DataFrame 1--------")
print(a1)
print("---------After Aligning to the inner DataFrame 2--------")
print(a2)
一旦我们运行程序,我们将得到以下结果。
-对准内部数据帧 1 - 名称辊无标记 1 Abhishek 100 90 2 Anurag 101 85 -对准内部数据帧 2 - 名称辊无标记 2 Chetan 103 75 3 Divya 104 80 4 Diya 105 92
示例:与index(axis=0)
上的Left
连接对齐
下面的示例显示了如何将两个不同的数据帧与索引上的左连接对齐。
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='left', axis=0)
print("---------After Aligning to the left with axis=0, DataFrame 1--------")
print(a1)
print("---------After Aligning to the left with axis=0, DataFrame 2--------")
print(a2)
一旦我们运行程序,我们将得到以下结果。
-与轴=0 对齐后向左,数据帧 1 - 名称卷无主题标记 1 Abhishek 100 Science 90 2 Anurag 101 Science 85 -与轴=0 对齐后向左,数据帧 2 - 名称卷无标记 1 NaN NaN 2 Chetan 103.0 75.0
结论
在本教程中,我们学习了 Pandas DataFrame.align()方法。我们通过不同的连接方法和轴来理解语法、参数和解决的例子。