数据对齐

import pandas as pd
import numpy as np

广播机制

DataFrame 对象之间的数据对齐在列和索引(行标签)上自动对齐。同样,结果对象将得到列和行标签的并集。

df1 = pd.DataFrame(np.random.randn(10, 4), columns=["A", "B", "C", "D"])
df1

ABCD
0-0.205518-1.0792440.4667540.697197
10.373488-0.704131-0.562608-0.747020
20.7277821.1769811.748998-2.381428
30.693188-0.236861-0.0453001.116772
40.679781-0.113023-0.2378970.991700
50.301815-0.736582-0.3278761.240366
6-0.3627920.409796-0.5590921.486711
71.1587632.898433-1.0669791.109399
81.801811-0.1509980.2561210.626933
90.248901-0.8469650.728085-0.302709
df2 = pd.DataFrame(np.random.randn(7, 3), columns=["A", "B", "C"])
df2

ABC
00.6562960.9484011.203846
10.5768052.193866-1.295344
20.107251-1.3946751.322735
30.278274-0.398505-0.894721
40.798450-0.8177461.933429
5-0.8561740.212137-0.323455
6-0.1482072.2930630.164304
df1 + df2

ABCD
00.450778-0.1308431.670600NaN
10.9502931.489735-1.857952NaN
20.835033-0.2176943.071733NaN
30.971461-0.635366-0.940020NaN
41.478231-0.9307691.695532NaN
5-0.554358-0.524445-0.651331NaN
6-0.5109982.702860-0.394789NaN
7NaNNaNNaNNaN
8NaNNaNNaNNaN
9NaNNaNNaNNaN

在 DataFrame 和 Series 之间执行操作时,默认行为是对齐 DataFrame 列上的 Series 索引,从而按行广播。例如:

df1 - df1.iloc[0]

ABCD
00.0000000.0000000.0000000.000000
10.5790070.375113-1.029362-1.444217
20.9333012.2562261.282244-3.078625
30.8987060.842383-0.5120540.419576
40.8853000.966221-0.7046510.294503
50.5073340.342662-0.7946300.543169
6-0.1572731.489041-1.0258460.789514
71.3642813.977677-1.5337330.412202
82.0073300.928246-0.210632-0.070263
90.4544200.2322790.261331-0.999906
Previous
Next