Will changes to DataFrame.values ​​always change values ​​in a data frame?

The documentation says:

Unnecessary NDFrame View - Source

What does "digital representation of NDFrame" mean? Will this change to this numpy view affect my original data structure. From my experiments so far it looks like this. np.diagonal(df.values, 0) sets all the values ​​in the diagonal part of df to 0. However, as shown in @ cᴏʟᴅsᴘᴇᴇᴅ's answer, a copy is sometimes returned.

It feels very thorough. This is a little strange for me, because I don't have a more detailed .values source.


EDIT . Another experiment in addition to the current experiments in @ cᴏʟᴅsᴘᴇᴇᴅ's answer:

 df = pd.DataFrame([["A", "B"],["C", "D"]]) df.values[0][0] = 0 

We get

 df 0 1 0 0 B 1 CD 

Although it is now mixed, we can still change the origal df by setting df.values

 df.values[0][1] = 5 df 0 1 0 0 5 1 CD 
+2
source share
1 answer

Let me check it out.

Firstly, with pd.Series objects.

 In [750]: s = pd.Series([1, 2, 3]) In [751]: v = s.values In [752]: v[0] = 10000 In [753]: s Out[753]: 0 10000 1 2 2 3 dtype: int64 

Now for DataFrame objects. First, consider non-mixed dtypes -

 In [780]: df = pd.DataFrame(1 - np.eye(3, dtype=int)) In [781]: df Out[781]: 0 1 2 0 0 1 1 1 1 0 1 2 1 1 0 In [782]: v = df.values In [783]: v[0] = 12345 In [784]: df Out[784]: 0 1 2 0 12345 12345 12345 1 1 0 1 2 1 1 0 

Modifications are made, therefore .values means return the view.

Now consider a mixed dtypes -

 In [755]: df = pd.DataFrame({'A' :[1, 2], 'B' : ['ccc', 'ddd']}) In [756]: df Out[756]: AB 0 1 ccc 1 2 ddd In [757]: v = df.values In [758]: v[0] = 123 In [759]: v[0, 1] = 'zzxxx' In [760]: df Out[760]: AB 0 1 ccc 1 2 ddd 

Here .values returns a copy.


Observation

.values for Series returns a view regardless of the type of each row, whereas for DataFrames it depends. For homogeneous types, the view is returned. Otherwise, a copy.

+3
source

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


All Articles