modin.pandas.DataFrame.loc¶
- property DataFrame.loc: _LocIndexer[source]¶
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.Allowed inputs are:
A single label, e.g.
5or'a', (note that5is interpreted as a label of the index, and never as an integer position along the index).A list or array of labels, e.g.
['a', 'b', 'c'].A slice object with labels, e.g.
'a':'f'.Warning
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, e.g.
[True, False, True].An alignable boolean Series. The index of the key will be aligned before masking.
An alignable Index. The Index of the returned selection will be the input.
A
callablefunction with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above)
Notes
To meet the nature of lazy evaluation:
Snowpark pandas
.locignores out-of-bounds indexing for row indexers (while pandas.locmay raise KeyError). If all values are out-of-bound, an empty result will be returned.Out-of-bounds indexing for columns will still raise a KeyError the same way pandas does.
In Snowpark pandas
.loc, unalignable boolean Series provided as indexer will perform a join on the index of the main dataframe or series. (while pandas will raise an IndexingError)When there is a slice key, Snowpark pandas
.locperforms the same as native pandas when both the start and stop are labels present in the index or either one is absent but the index is sorted. When any of the two labels is absent from an unsorted index, Snowpark pandas will return rows in between while native pandas will raise a KeyError.Special indexing for DatetimeIndex is unsupported in Snowpark pandas, e.g., partial string indexing.
While setting rows with duplicated index, Snowpark pandas won’t raise ValueError for duplicate labels to avoid eager evaluation.
When using
.locto set values with a Series key and Series item, the index of the item is ignored, and values are set positionally.pandas
.locmay sometimes raise a ValueError when using.locto set values in a DataFrame from a Series using a Series as the column key, but Snowpark pandas.locsupports this type of operation according to the rules specified above..locwith boolean indexers for columns is currently unsupported.When using
.locto set column values for a Series item, with aslice(None)for the row columns, Snowpark pandas sets the value for each row from the Series.
See also
DataFrame.atAccess a single value for a row/column label pair.
DataFrame.ilocAccess group of rows and columns by integer position(s).
DataFrame.xsReturns a cross-section (row(s) or column(s)) from the Series/DataFrame.
Series.locAccess group of values using labels.
Examples
Getting values
Single label. Note this returns the row as a Series.
List of labels. Note using
[[]]returns a DataFrame.Single label for row and column
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.
Boolean list with the same length as the row axis
Alignable boolean Series:
Index (same behavior as
df.reindex)Conditional that returns a boolean Series
Conditional that returns a boolean Series with column labels specified
Callable that returns a boolean Series
Setting values
Set value for all items matching the list of labels
Set value for an entire row
Set value for an entire column
Set value for rows matching callable condition
Setting the values with a Series item.
Getting values on a DataFrame with an index that has integer labels
Another example using integers for the index
Slice with integer labels for rows. As mentioned above, note that both the start and stop of the slice are included.
Getting values with a MultiIndex
A number of examples using a DataFrame with a MultiIndex
Single label. Note this returns a DataFrame with a single index.
Single index tuple. Note this returns a Series.
Single label for row and column. Similar to passing in a tuple, this returns a Series.
Single tuple. Note using
[[]]returns a DataFrame.Single tuple for the index with a single label for the column
Slice from index tuple to single label
Slice from index tuple to index tuple
Set column values from Series with Series key.
Set column values from Series with list key.
Set column values for all rows from Series item.