pandas focuses on the structures of tabular data, and when performing operations (addition, subtraction, etc.), it looks at the labels - not at the position.
DataFrame:
df = pd.DataFrame(np.random.randn(5, 3), index=list('abcde'), columns=list('xyz'))
df[1:]
:
df[1:]
Out:
x y z
b 1.003035 0.172960 1.160033
c 0.117608 -1.114294 -0.557413
d -1.312315 1.171520 -1.034012
e -0.380719 -0.422896 1.073535
df[:-1]
:
df[:-1]
Out:
x y z
a 1.367916 1.087607 -0.625777
b 1.003035 0.172960 1.160033
c 0.117608 -1.114294 -0.557413
d -1.312315 1.171520 -1.034012
df[1:] / df[:-1]
, b
b
, c
c
d
d
. a
e
DataFrame ( , ), nan
:
df[1:] / df[:-1]
Out:
x y z
a NaN NaN NaN
b 1.0 1.0 1.0
c 1.0 1.0 1.0
d 1.0 1.0 1.0
e NaN NaN NaN
, , numpy .values
- pandas . numpy , pandas :
df[1:]/df[:-1].values
Out:
x y z
b 0.733258 0.159028 -1.853749
c 0.117252 -6.442482 -0.480515
d -11.158359 -1.051357 1.855018
e 0.290112 -0.360981 -1.038223