Change pandas DataFrame using indexes

Is there a general efficient way to assign values ​​to a subset of a DataFrame in pandas? I have hundreds of rows and columns that I can access directly, but I was not able to figure out how to edit their values ​​without repeating through each pair of rows, col. For instance:

In [1]: import pandas, numpy In [2]: array = numpy.arange(30).reshape(3,10) In [3]: df = pandas.DataFrame(array, index=list("ABC")) In [4]: df Out[4]: 0 1 2 3 4 5 6 7 8 9 A 0 1 2 3 4 5 6 7 8 9 B 10 11 12 13 14 15 16 17 18 19 C 20 21 22 23 24 25 26 27 28 29 In [5]: rows = ['A','C'] In [6]: columns = [1,4,7] In [7]: df[columns].ix[rows] Out[7]: 1 4 7 A 1 4 7 C 21 24 27 In [8]: df[columns].ix[rows] = 900 In [9]: df Out[9]: 0 1 2 3 4 5 6 7 8 9 A 0 1 2 3 4 5 6 7 8 9 B 10 11 12 13 14 15 16 17 18 19 C 20 21 22 23 24 25 26 27 28 29 

I believe that what happens here is that I get a copy, not a view, that is, I can not assign the original DataFrame. That's my problem? What is the most efficient way to edit these rows x columns (preferably at a pace since a DataFrame can take up a lot of memory)?

Also, what if I want to replace these values ​​with a properly formed DataFrame?

+6
source share
1 answer

Use loc in the assignment expression ( = means this does not apply to whether it is a view or a copy!)

 In [11]: df.loc[rows, columns] = 99 In [12]: df Out[12]: 0 1 2 3 4 5 6 7 8 9 A 0 99 2 3 99 5 6 99 8 9 B 10 11 12 13 14 15 16 17 18 19 C 20 99 22 23 99 25 26 99 28 29 

If you are using version prior to 0.11, you can use .ix .

Like @Jeff's comments:

This is an assignment expression (see the 'advanced indexing with ix' section of the docs ) and returns nothing (although there are assignment expressions that do return things like .at and .iat ).

df.loc[rows,columns] may return a view, but it is usually a copy. Confusing, but done for efficiency.

Bottom line: use ix , loc , iloc to install (as described above) and not modify copies of strong>.

See the section 'view versus copy' in the docs.

+10
source

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


All Articles