Pandas vs Digital Data Frames

Take a look at these few lines of code:

df2 = df.copy()
df2[1:] = df[1:]/df[:-1].values -1
df2.ix[0, :] = 0

Our instructor told us that we need to use the attribute .values for access to basic array numpy, otherwise our code will not work.

I understand that pandas DataFrame has a basic representation as a numpy array, but I did not understand why we cannot work directly with pandas DataFrame using only a cut.

Can you tell me about this?

+6
source share
1 answer

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
+7

Source: https://habr.com/ru/post/1017062/


All Articles