Pandas DataFrame.align(~) 方法确保两个 DataFrames 具有相同的列或行标签。
参数
1.other | DataFrame 或 Series
您想要对齐的 DataFrame 或系列。
2. join | string | optional
要执行的连接类型:
-
"outer" -
"inner" -
"left" -
"right"
默认情况下,join="outer" 。请参阅下面的示例以进行说明。
3. axis | None 或 int 或 string | optional
执行对齐的轴:
|
轴 |
说明 |
|---|---|
|
|
使用行标签对齐 |
|
|
使用列标签对齐 |
默认情况下,axis=None 。
4. level | int 或 string | optional
目标水平。这仅与多索引数据帧相关。默认情况下,level=None 。
5. copy | boolean | optional
是否返回新副本。如果 copy=False 且未执行重新索引,则将返回原始 DataFrames/Series。默认情况下,copy=True 。
6. fill_value | scalar | optional
用于填充缺失值的值 (NaN)。默认情况下, fill_value=np.NaN ,即缺失值保持原样。
7. method | None 或 string | optional
填补缺失值的方法:
|
方法 |
说明 |
|---|---|
|
|
使用之前的有效观察进行填写 |
|
|
使用下一个有效观察进行填充 |
默认情况下,method=None 。
8. limit | int | optional
允许的最大连续填充次数。例如,如果您有 3 个连续的 NaN ,并且设置了 limit=2 ,则只会填充前两个 NaN ,第三个将保持原样。默认情况下,limit=None 。
9. fill_axis | int 或 string | optional
是否水平或垂直应用method:
|
轴 |
说明 |
|---|---|
|
|
填充是垂直应用的。 |
|
|
填充是水平应用的。 |
默认情况下,fill_axis=0 。
10.broadcast_axis | int 或 string | optional
执行广播的轴:
|
轴 |
说明 |
|---|---|
|
|
沿索引轴广播。 |
|
|
沿列轴广播。 |
默认情况下,broadcast_axis=None 。仅当源 DataFrame 和 other 具有不同尺寸时,这才相关。
返回值
DataFrames 的 sized-two 元组(对齐的源 DataFrame、其他 DataFrame/Series)。
例子
指定连接类型
考虑以下两个 DataFrame:
df_one = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df_two = pd.DataFrame({"A":[7,8], "E":[9,10], "B":[11,12]}, index=["a","b"])
[df_one] [df_two]
A B C A E B
0 1 3 5 a 7 9 11
1 2 4 6 b 8 10 12
外full-join
要通过外部full-join对齐两个DataFrame:
a_one, a_two = df_one.align(df_two, axis=1) # join="outer"
[a_one] | [a_two]
A B C E | A B C E
0 1 3 5 NaN | a 7 12 NaN 9
1 2 4 6 NaN | b 8 12 NaN 10
在此,请注意以下事项:
-
默认情况下,
join="outer",这意味着生成的 DataFrames 将具有两个输入 DataFrame 中存在的所有列标签。这就是我们在a_one中看到列标签E和在a_two中看到列标签C的原因。 -
axis=1参数告诉 Pandas 按列执行对齐。 -
尽管添加了新列,但它们不包含任何值,因为它们填充了
NaN。
内部联接
通过 inner-join 对齐:
a_one, a_two = df_one.align(df_two, join="inner", axis=1)
[a_one] [a_two]
A B A B
0 1 3 a 7 11
1 2 4 b 8 12
我们获得此结果是因为 DataFrames 中都存在列标签 "A" 和 "B" - 所有其他列都被剥离。
左连接
通过 left-join 对齐:
a_one, a_two = df_one.align(df_two, join="left", axis=1)
a_one
[a_one] [a_two]
A B C A B C
0 1 3 5 a 7 11 NaN
1 2 4 6 b 8 12 NaN
通过执行左连接,我们确保 other DataFrame 具有源 DataFrame 的所有列标签。这就是为什么我们看到 C 列出现在 a_two 中。
指定轴
再次假设我们有以下两个 DataFrame:
df_one = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df_two = pd.DataFrame({"A":[7,8], "E":[9,10], "B":[11,12]}, index=["a","b"])
[df_one] [df_two]
A B C A E B
0 1 3 5 a 7 9 11
1 2 4 6 b 8 10 12
轴=0
a_one, a_two = df_one.align(df_two, axis=0)
[a_one] [a_two]
A B C A E B
0 1.0 3.0 5.0 0 NaN NaN NaN
1 2.0 4.0 6.0 1 NaN NaN NaN
a NaN NaN NaN a 7.0 9.0 11.0
b NaN NaN NaN b 8.0 10.0 12.0
通过设置axis=0,我们告诉 Pandas对齐行标签,也就是说,两个结果 DataFrames 具有完全相同的行标签。但是,请注意两个 DataFrame 的列标签如何保持完整。
轴=1
a_one, a_two = df_one.align(df_two, axis=1)
[a_one] | [a_two]
A B C E | A B C E
0 1 3 5 NaN | a 7 12 NaN 9
1 2 4 6 NaN | b 8 12 NaN 10
通过设置axis=1,我们告诉 Pandas对齐列标签,也就是说,两个生成的 DataFrames 具有完全相同的列标签。但是,请注意两个 DataFrame 的行标签如何保持完整。
轴=无
默认参数值为axis=None:
a_one, a_two = df_one.align(df_two) # axis=None
[a_one] [a_two]
A B C E A B C E
0 1.0 3.0 5.0 NaN 0 NaN NaN NaN NaN
1 2.0 4.0 6.0 NaN 1 NaN NaN NaN NaN
a NaN NaN NaN NaN a 7.0 11.0 NaN 9.0
b NaN NaN NaN NaN b 8.0 12.0 NaN 10.0
axis=None 是 axis=0 和 axis=1 的组合,也就是说,生成的 DataFrames 将共享相同的行标签和列标签。
进行灌装
考虑我们之前的DataFrames:
df_one = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df_two = pd.DataFrame({"A":[7,8], "E":[9,10], "B":[11,12]}, index=["a","b"])
[df_one] [df_two]
A B C A E B
0 1 3 5 a 7 9 11
1 2 4 6 b 8 10 12
使用外部 full-join 执行水平对齐会产生:
a_one, a_two = df_one.align(df_two, axis=1) # join="outer"
[a_one] | [a_two]
A B C E | A B C E
0 1 3 5 NaN | a 7 12 NaN 9
1 2 4 6 NaN | b 8 12 NaN 10
请注意,由于默认情况下不执行填充,因此我们最终会得到缺失值。
要填充 NaN ,我们可以指定参数 method 和可选的 fill_axis :
a_one, a_two = df_one.align(df_two, axis=1, method="ffill", fill_axis=1)
a_one, a_two
[a_one] | [a_two]
A B C E | A B C E
0 1.0 3.0 5.0 5.0 | a 7.0 11.0 11.0 9.0
1 2.0 4.0 6.0 6.0 | b 8.0 12.0 12.0 10.0
在此,请注意以下事项:
-
method="ffill"应用前向填充,这意味着NaN使用之前的有效观察值进行填充。 -
fill_axis=1执行水平前向填充。
相关用法
- Python PySpark DataFrame alias方法用法及代码示例
- Python Pandas DataFrame all方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame add方法用法及代码示例
- Python Pandas DataFrame asfreq方法用法及代码示例
- Python Pandas DataFrame any方法用法及代码示例
- Python Pandas DataFrame append方法用法及代码示例
- Python Pandas DataFrame asof方法用法及代码示例
- Python Pandas DataFrame add_prefix方法用法及代码示例
- Python Pandas DataFrame add_suffix方法用法及代码示例
- Python Pandas DataFrame at属性用法及代码示例
- Python Pandas DataFrame axes属性用法及代码示例
- Python Pandas DataFrame astype方法用法及代码示例
- Python Pandas DataFrame assign方法用法及代码示例
- Python Pandas DataFrame apply方法用法及代码示例
- Python Pandas DataFrame applymap方法用法及代码示例
- Python Pandas DataFrame at_time方法用法及代码示例
- Python Pandas DataFrame abs方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | align method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
