SoFunction
Updated on 2024-10-29

python functions in detail

official function


Access a group of rows and columns by label(s) or a boolean array.
.loc[] is primarily label based, but may also be used with a boolean array.
# You can use label values, but you can also use boolean values

  • Allowed inputs are: # Single labels, lists of multiple labels, slices of multiple labels can be accepted.
  • A single label, . 5 or ‘a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index). #Here.5Not the position specified by the value,ratherlabel(be) worth
  • A list or array of labels, . [‘a', ‘b', ‘c'].

slice object with labels, . ‘a':'f'.

Warning: #If slices with multiple labels are used, then the slices all start at the contained position

Note that contrary to usual python slices, both the start and the stop are included

  • A boolean array of the same length as the axis being sliced, . [True, False, True].

Illustrative examples

I. Selection of values

1. Generate df

df = ([[1, 2], [4, 5], [7, 8]],
...   index=['cobra', 'viper', 'sidewinder'],
...   columns=['max_speed', 'shield'])

df
Out[15]: 
      max_speed shield
cobra        1    2
viper        4    5
sidewinder     7    8

2、Single label. single row_label returned Series

['viper']
Out[17]: 
max_speed  4
shield    5
Name: viper, dtype: int64

2, List of labels. list row_label returned DataFrame

[['cobra','viper']]
Out[20]: 
    max_speed shield
cobra     1    2
viper     4    5

3、Single label for row and column Select both rows and columns

['cobra', 'shield']
Out[24]: 2

4、Slice with labels for row and single label for column. As mentioned above, note that both the start and stop of the slice are included. Select multiple rows and individual columns at the same time,Note that selecting multiplerow label hour,The first place is chosen.。

['cobra':'viper', 'max_speed']
Out[25]: 
cobra  1
viper  4
Name: max_speed, dtype: int64

5、Boolean list with the same length as the row axis Boolean list selectionrow label
The list of Boolean values is selected based on the True or False of a position. If the Boolean value of a position is True, the row is selected.

df
Out[30]: 
      max_speed shield
cobra        1    2
viper        4    5
sidewinder     7    8

[[True]]
Out[31]: 
    max_speed shield
cobra     1    2

[[True,False]]
Out[32]: 
    max_speed shield
cobra     1    2

[[True,False,True]]
Out[33]: 
      max_speed shield
cobra        1    2
sidewinder     7    8

6、Conditional that returns a boolean Series conditional boolean

[df['shield'] > 6]
Out[34]: 
      max_speed shield
sidewinder     7    8

7、Conditional that returns a boolean Series with column labels specified Conditional Boolean values and data for a specific column

[df['shield'] > 6, ['max_speed']]
Out[35]: 
      max_speed
sidewinder     7

8, Callable that returns a boolean Series through the function to get boolean results selected data

df
Out[37]: 
      max_speed shield
cobra        1    2
viper        4    5
sidewinder     7    8

[lambda df: df['shield'] == 8]
Out[38]: 
      max_speed shield
sidewinder     7    8

II. Assignment

1、Set value for all items matching the list of labels Selected from a list ofrow cohort column assign a value to something

[['viper', 'sidewinder'], ['shield']] = 50

df
Out[43]: 
      max_speed shield
cobra        1    2
viper        4   50
sidewinder     7   50

2、Set value for an entire row treat a line of work asrowAll the data in the

['cobra'] =10

df
Out[48]: 
      max_speed shield
cobra       10   10
viper        4   50
sidewinder     7   50

3、Set value for an entire column will be a column of data completely assigned value

[:, 'max_speed'] = 30

df
Out[50]: 
      max_speed shield
cobra       30   10
viper       30   50
sidewinder     30   50

4、Set value for rows matching callable condition Conditions selectedrowsassign a value to something

[df['shield'] > 35] = 0

df
Out[52]: 
      max_speed shield
cobra       30   10
viper        0    0
sidewinder     0    0

III. Row indexes are numeric

df = ([[1, 2], [4, 5], [7, 8]],
...   index=[7, 8, 9], columns=['max_speed', 'shield'])

df
Out[54]: 
  max_speed shield
7     1    2
8     4    5
9     7    8

Takes multiple slices through rows of rows:

[7:9]
Out[55]: 
  max_speed shield
7     1    2
8     4    5
9     7    8

IV. Multidimensional indexing

1, generate multi-dimensional index

tuples = [
...  ('cobra', 'mark i'), ('cobra', 'mark ii'),
...  ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'),
...  ('viper', 'mark ii'), ('viper', 'mark iii')
... ]
index = .from_tuples(tuples)
values = [[12, 2], [0, 4], [10, 20],
...     [1, 4], [7, 1], [16, 36]]
df = (values, columns=['max_speed', 'shield'], index=index)


df
Out[57]: 
           max_speed shield
cobra   mark i      12    2
      mark ii      0    4
sidewinder mark i      10   20
      mark ii      1    4
viper   mark ii      7    1
      mark iii     16   36

2, Single label. passed into the outermost row label, return DataFrame

['cobra']
Out[58]: 
     max_speed shield
mark i     12    2
mark ii     0    4

3, Single index tuple. Passed in the index tuple, return Series

[('cobra', 'mark ii')]
Out[59]: 
max_speed  0
shield    4
Name: (cobra, mark ii), dtype: int64

4, Single label for row and column. If you pass in row and column, and pass in tuple is similar, return Series

['cobra', 'mark i']
Out[60]: 
max_speed  12
shield    2
Name: (cobra, mark i), dtype: int64

5. Single tuple. Note using [[ ]] returns a DataFrame. pass in an array, return a DataFrame

[[('cobra', 'mark ii')]]
Out[61]: 
        max_speed shield
cobra mark ii     0    4

6、Single tuple for the index with a single label for the column Getting acoluma certainrowdata,Requires a left-left pass to a multidimensional indextuple,Then pass in thecolumn

[('cobra', 'mark i'), 'shield']
Out[62]: 2

7. Pass in slices for multidimensional indexes and single indexes:

[('cobra', 'mark i'):'viper']
Out[63]: 
           max_speed shield
cobra   mark i      12    2
      mark ii      0    4
sidewinder mark i      10   20
      mark ii      1    4
viper   mark ii      7    1
      mark iii     16   36

[('cobra', 'mark i'):'sidewinder']
Out[64]: 
          max_speed shield
cobra   mark i     12    2
      mark ii     0    4
sidewinder mark i     10   20
      mark ii     1    4

[('cobra', 'mark i'):('sidewinder','mark i')]
Out[65]: 
          max_speed shield
cobra   mark i     12    2
      mark ii     0    4
sidewinder mark i     10   20

to this article on the use of python functions explained the article is introduced to this, more related functions, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future!