The axis labeling information in pandas objects serves many purposes:
Identifies data (i.e. provides metadata) using known indicators,
important for analysis, visualization, and interactive console display.
Enables automatic and explicit data alignment.
Allows intuitive getting and setting of subsets of the data set.
In this section, we will focus on the final point: namely, how to slice, dice,
and generally get and set subsets of pandas objects. The primary focus will be
on Series and DataFrame as they have received more development attention in
this area.
Object selection has had a number of user-requested additions in order to
support more explicit location based indexing. pandas now supports three types
of multi-axis indexing.
.loc is primarily label based, but may also be used with a boolean array. .loc will raise KeyError when the items are not found. Allowed inputs are:
A single label, e.g. 5 or 'a' (Note that 5 is interpreted as a
label of the index. This use is not an integer position along the
index.).
A list or array of labels ['a','b','c'].
A slice object with labels 'a':'f' (Note that contrary to usual Python
slices, both the start and the stop are included, when present in the
index! See Slicing with labels
and Endpoints are inclusive.)
A boolean array (any NA values will be treated as False).
A callable function with one argument (the calling Series or DataFrame) and
that returns valid output for indexing (one of the above).
A tuple of row (and column) indices whose elements are one of the
above inputs.
.iloc is primarily integer position based (from 0 to
length-1 of the axis), but may also be used with a boolean
array. .iloc will raise IndexError if a requested
indexer is out-of-bounds, except slice indexers which allow
out-of-bounds indexing. (this conforms with Python/NumPy slice
semantics). Allowed inputs are:
An integer e.g. 5.
A list or array of integers [4,3,0].
A slice object with ints 1:7.
A boolean array (any values will be treated as ).
.loc, .iloc, and also [] indexing can accept a callable as indexer. See more at Selection By Callable.
Getting values from an object with multi-axes selection uses the following
notation (using .loc as an example, but the following applies to .iloc as
well). Any of the axes accessors may be the null slice :. Axes left out of
the specification are assumed to be :, e.g. p.loc['a'] is equivalent to
p.loc['a',:].
In [1]: ser=pd.Series(range(5),index=list("abcde"))In [2]: ser.loc[["a","c","e"]]Out[2]: a 0c 2e 4dtype: int64In [3]: df=pd
Basics
As mentioned when introducing the data structures in the last section, the primary function of indexing with [] (a.k.a. __getitem__
for those familiar with implementing class behavior in Python) is selecting out
lower-dimensional slices. The following table shows return type values when
indexing pandas objects with []:
Object Type
Selection
Return Value Type
Series
series[label]
scalar value
DataFrame
frame[colname]
Series corresponding to colname
Here we construct a simple time series data set to use for illustrating the
indexing functionality:
In [5]: dates=pd.date_range('1/1/2000',periods=8)In [6]: df=pd.DataFrame(np.random.randn(8,4), ...: index=dates,columns=[
Thus, as per above, we have the most basic indexing using []:
In [8]: s=df['A']In [9]: s[dates[5]]Out[9]: -0.6736897080883706
You can pass a list of columns to [] to select columns in that order.
If a column is not contained in the DataFrame, an exception will be
raised. Multiple columns can also be set in this manner:
In [10]: dfOut[10]: A B C D2000-01-01 0.469112 -0.282863 -1.509059 -1.1356322000-01-02 1.212112 -0.173215 0.119209 -1.0442362000-01-03 -0.861849 -2.104569 -0.494929 1.0718042000-01-04 0.721555 -0.706771 -1.039575 0.2718602000-01-05 -0.424972 0.567020 0.276232 -1.0874012000-01-06 -0.673690 0.113648 -1.478427 0.5249882000-01-07 0.404705 0.577046 -1.715002 -1.0392682000-01-08 -0.370647 -1.157892 -1.344312 0.844885In [11]: df[['B','A']]=df[['A','B']]In [12]: dfOut[12]: A B C D
You may find this useful for applying a transform (in-place) to a subset of the
columns.
Attribute access
You may access an index on a Series or column on a DataFrame directly
as an attribute:
In [21]: sa=pd.Series([1,2,3],index=list('abc'))In [22]: dfa=df.copy()
In [23]: sa.bOut[23]: 2In [24]: dfa.AOut[24]: 2000-01-01 -0.2828632000-01-02 -0.1732152000-01-03 -2.1045692000-01-04 -0.7067712000-01-05 0.5670202000-01-06 0.1136482000-01-07 0.5770462000-01-08 -1.157892Freq: D, Name: A, dtype: float64
In [25]: sa.a=5In [26]: saOut[26]: a 5b 2c 3dtype: int64In [27]: dfa.A=list(range(len(dfa.index)))# ok if A already existsIn [28]: dfaOut[28]: A B C D2000-01-01 0 0.469112 -1.509059 -1.1356322000-01-02 1 1.212112 0.119209 -1.044236
If you are using the IPython environment, you may also use tab-completion to
see these accessible attributes.
You can also assign a dict to a row of a DataFrame:
In [31]: x=pd.DataFrame({'x':[1,2,3],'y':[3,4,5]})In [32]: x.iloc[1]={'x':9,
You can use attribute access to modify an existing element of a Series or column of a DataFrame, but be careful;
if you try to use attribute access to create a new column, it creates a new attribute rather than a
new column and will this raise a UserWarning:
The most robust and consistent way of slicing ranges along arbitrary axes is
described in the Selection by Position section
detailing the .iloc method. For now, we explain the semantics of slicing using the [] operator.
With Series, the syntax works exactly as with an ndarray, returning a slice of
the values and the corresponding labels:
With DataFrame, slicing inside of []slices the rows. This is provided
largely as a convenience since it is such a common operation.
In [43]: df[:3]Out[43]: A B C D2000-01-01 -0.282863 0.469112 -1.509059 -1.1356322000-01-02 -0.173215 1.212112 0.119209 -1.0442362000-01-03 -2.104569 -0.861849 -0.494929 1.071804In [44]: df[::-1]Out[44]: A B C D2000-01-08 -1.157892 -0.370647 -1.344312 0.8448852000-01-07 0.577046 0.404705 -1.715002 -1.0392682000-01-06 0.113648 -0.673690 -1.478427 0.5249882000-01-05 0.567020 -0.424972 0.276232 -1.0874012000-01-04 -0.706771 0.721555 -1.039575 0.2718602000-01-03 -2.104569 -0.861849 -0.494929 1.0718042000-01-02 -0.173215 1.212112 0.119209 -1.0442362000-01-01 -0.282863 0.469112 -1.509059 -1.135632
Selection by label
pandas provides a suite of methods in order to have purely label based indexing. This is a strict inclusion based protocol.
Every label asked for must be in the index, or a KeyError will be raised.
When slicing, both the start bound AND the stop bound are included, if present in the index.
Integers are valid labels, but they refer to the label and not the position.
The .loc attribute is the primary access method. The following are valid inputs:
A single label, e.g. 5 or 'a' (Note that 5 is interpreted as a label of the index. This use is not an integer position along the index.).
A list or array of labels ['a','b','c'].
A slice object with labels 'a':'f' (Note that contrary to usual Python
slices, both the start and the stop are included, when present in the
index! See Slicing with labels.
In [49]: s1=pd.Series(np.random.randn(6),index=list('abcdef'))In [50]: s1Out[50]: a 1.431256b 1.340309c -1.170299d -0.226169e 0.410835f 0.813850dtype: float64In [51]: s1.loc['c'
Note that setting works as well:
In [53]: s1.loc['c':]=0In [54]: s1Out[54]: a 1.431256b 1.340309c 0.000000d 0.000000e 0.000000f 0.000000dtype: float64
With a DataFrame:
In [55]: df1=pd.DataFrame(np.random.randn(6,4), ....: index=list('abcdef'), ....: columns=list('ABCD')) ....: In [56]: df1Out[56]: A B C D
Accessing via label slices:
In [58]: df1.loc['d':,'A':'C']Out[58]: A B Cd 0.974466 -2.006747 -0.410001e 0.545952 -1.219217 -1.226825f -1.281247 -0.727707 -0.121306
For getting a cross section using a label (equivalent to df.xs('a')):
In [59]: df1.loc['a']Out[59]: A 0.132003B -0.827317C -0.076467D -1.187678Name: a, dtype: float64
For getting values with a boolean array:
In [60]: df1.loc['a']>0Out[60]: A TrueB FalseC FalseD FalseName: a, dtype: boolIn [61]: df1.loc[:,df1.loc['a']>0]Out[61]: Aa 0.132003b 1.130127c 1.024180d 0.974466e 0.545952
# this is also equivalent to ``df1.at['a','A']``In [65]: df1.loc['a','A']Out[65]: 0.13200317033032932
Slicing with labels
When using .loc with slices, if both the start and the stop labels are
present in the index, then elements located between the two (including them)
are returned:
In [66]: s=pd.Series(list('abcde'),index=[0,3,2,5,4])In [67]: s
Selection by position
pandas provides a suite of methods in order to get purely integer based indexing. The semantics follow closely Python and NumPy slicing. These are 0-based indexing. When slicing, the start bound is included, while the upper bound is excluded. Trying to use a non-integer, even a valid label will raise an IndexError.
The .iloc attribute is the primary access method. The following are valid inputs:
Out of range slice indexes are handled gracefully just as in Python/NumPy.
# these are allowed in Python/NumPy.In [87]: x=list('abcdef')In [88]: xOut[88]: ['a', 'b', 'c', 'd', 'e', 'f']In [89]: x[4:10]Out[89]: ['e', 'f']In [90]: x[8:10]Out[90]: []In [91]: s=pd.Series
Note that using slices that go out of bounds can result in
an empty axis (e.g. an empty DataFrame being returned).
In [95]: dfl=pd.DataFrame(np.random.randn(5,2),columns=list('AB'))In [96]: dflOut[96]: A B0 -0.082240 -2.1829371 0.380396 0.0848442 0.432390 1.5199703 -0.493662 0.6001784 0.274230 0.132885In [97]: dfl.iloc
A single indexer that is out of bounds will raise an IndexError.
A list of indexers where any element is out of bounds will raise an
IndexError.
In [100]: dfl.iloc[[4,5,6]]---------------------------------------------------------------------------IndexErrorTraceback (most recent call last)
File ~/work/pandas/pandas/pandas/core/indexing.py:1715, in _iLocIndexer._get_list_axis(self, key, axis)1714try:-> 1715returnself.obj._take_with_is_copy(key,axis=axis
in
in
in
in
: indices are out-of-bounds
Traceback (most recent call last)
in
in
in
: positional indexers are out-of-bounds
In [101]: dfl.iloc[:,4]---------------------------------------------------------------------------IndexErrorTraceback (most recent call last)
CellIn[101],line1----> 1dfl.iloc[:,4]File ~/work/pandas/pandas/pandas/core/indexing.py:1185, in _LocationIndexer.__getitem__(self, key)1183ifself._is_scalar_access(
in
in
in
in
: single positional indexer is out-of-bounds
Selection by callable
.loc, .iloc, and also [] indexing can accept a callable as indexer.
The callable must be a function with one argument (the calling Series or DataFrame) that returns valid output for indexing.
In [102]: df1=pd.DataFrame(np.random.randn(6,4), .....: index=list('abcdef'), .....: columns=list('ABCD')) .....: In [103]: df1Out[103]: A B C Da -0.023688 2.410179 1.450520 0.206053b -0.251905 -2.213588 1.063327 1.266143c 0.299368 -0.863838 0.408204 -1.048089d -0.025747 -0.988387 0.094055 1.262731e 1.289997 0.082423 -0.055758 0.536580f -0.489682 0.369374 -0.034571 -2.484478In [104]: df1.loc[lambdadf:df['A']>0,:]Out[104]: A B C Dc 0.299368 -0.863838 0.408204 -1.048089e 1.289997 0.082423 -0.055758 0.536580In [105]: df1.loc[:,lambdadf:['A','B']]Out[105]: A Ba -0.023688 2.410179b -0.251905 -2.213588c 0.299368 -0.863838d -0.025747 -0.988387e 1.289997 0.082423f -0.489682 0.369374In [106]: df1.iloc[:,lambdadf:[0,1]]Out[106]: A Ba -0.023688 2.410179b -0.251905 -2.213588c 0.299368 -0.863838d -0.025747 -0.988387e 1.289997 0.082423f -0.489682 0.369374In [107]: df1[lambdadf:df.columns[0]]Out[107]: a -0.023688b -0.251905c 0.299368d -0.025747e 1.289997f -0.489682Name: A, dtype: float64
You can use callable indexing in Series.
In [108]: df1['A'].loc[lambdas:s>0]Out[108]: c 0.299368e 1.289997Name: A, dtype: float64
Using these methods / indexers, you can chain data selection operations
without using a temporary variable.
In [109]: bb=pd.read_csv('data/baseball.csv',index_col='id')In [110]: (bb.groupby(['year','team']).sum(numeric_only=True) .....: .loc[lambdadf:
Combining positional and label-based indexing
If you wish to get the 0th and the 2nd elements from the index in the ‘A’ column, you can do:
In [111]: dfd=pd.DataFrame({'A':[1,2,3], .....: 'B':[4,5,6]}, .....: index=list('abc')) .....: In [112]: dfdOut[112]: A Ba 1 4b 2 5c 3 6In [113]: dfd.loc[dfd.index[[0,2]],'A']Out[113]: a 1c 3Name: A, dtype: int64
This can also be expressed using .iloc, by explicitly getting locations on the indexers, and using
positional indexing to select things.
In [114]: dfd.iloc[[0,2],dfd.columns.get_loc('A')]Out[114]: a 1c 3Name: A, dtype: int64
For getting multiple indexers, using .get_indexer:
In [115]: dfd.iloc[[0,2],dfd.columns.get_indexer(['A','B'])]Out[115]: A Ba 1 4c 3 6
Reindexing
The idiomatic way to achieve selecting potentially not-found elements is via .reindex(). See also the section on reindexing.
In [116]: s=pd.Series([1,2,3])In [117]: s.reindex([1,2,3])Out[117]:
Selecting random samples
A random selection of rows or columns from a Series or DataFrame with the sample() method. The method will sample rows by default, and accepts a specific number of rows/columns to return, or a fraction of rows.
In [126]: s=pd.Series([0,1,2,3,4,5])# When no arguments are passed, returns 1 row.In [127]: s.sample()Out[127]: 4 4dtype: int64# One may specify either a number of rows:In [128]: s.sample(n=3)Out[128]: 0 04 41 1dtype: int64# Or a fraction of the rows:In [129]: s.sample(frac=0.5)Out[129]: 5 53 31 1dtype: int64
By default, sample will return each row at most once, but one can also sample with replacement
using the replace option:
In [130]: s=pd.Series([0,1,2,3,4,5])# Without replacement (default):In [131]: s.sample(n=6,replace=False)Out[131]: 0 01 15 5
By default, each row has an equal probability of being selected, but if you want rows
to have different probabilities, you can pass the sample function sampling weights as
weights. These weights can be a list, a NumPy array, or a Series, but they must be of the same length as the object you are sampling. Missing values will be treated as a weight of zero, and inf values are not allowed. If weights do not sum to 1, they will be re-normalized by dividing all weights by the sum of the weights. For example:
In [133]: s=pd.Series([0,1,2,3,4,5])In [134]: example_weights=[0,0,0.2,0.2,0.2,0.4]In [135]: s.
When applied to a DataFrame, you can use a column of the DataFrame as sampling weights
(provided you are sampling rows and not columns) by simply passing the name of the column
as a string.
In [138]: df2=pd.DataFrame({'col1':[9,8,7,6], .....: 'weight_column':[0.5,0.4,0.1,0]}) .....: In [139]: df2.sample(n
sample also allows users to sample columns instead of rows using the axis argument.
In [140]: df3=pd.DataFrame({'col1':[1,2,3],'col2':[2,3,4]})In [141]: df3.sample(n=1,axis=1)
Finally, one can also set a seed for sample’s random number generator using the random_state argument, which will accept either an integer (as a seed) or a NumPy RandomState object.
In [142]: df4=pd.DataFrame({'col1':[1,2,3],'col2':[2,3,4]})# With a given seed, the sample will always draw the same rows.In [143]: df4.sample(n=2,random_state=
Setting with enlargement
The .loc/[] operations can perform enlargement when setting a non-existent key for that axis.
In the Series case this is effectively an appending operation.
A DataFrame can be enlarged on either axis via .loc.
In [149]: dfi=pd.DataFrame(np.arange(6).reshape(3,2), .....: columns=['A','B']) .....: In [150]: dfiOut[150]: A B0 0 11 2 32 4 5In [151]:
This is like an append operation on the DataFrame.
In [153]: dfi.loc[3]=5In [154]: dfiOut[154]: A B C0 0 1 01 2 3 22 4 5 43 5 5 5
Fast scalar value getting and setting
Since indexing with [] must handle a lot of cases (single-label access,
slicing, boolean indexing, etc.), it has a bit of overhead in order to figure
out what you’re asking for. If you only want to access a scalar value, the
fastest way is to use the at and iat methods, which are implemented on
all of the data structures.
Similarly to loc, at provides label based scalar lookups, while, iat provides integer based lookups analogously to iloc
In [155]: s.iat[5]Out[155]: 5In [156]: df.at[dates[5],'A']Out[156]: 0.1136484096888855In [157]: df.iat[3,0]Out[157]: -0.7067711336300845
You can also set using these same indexers.
In [158]: df.at[dates[5],'E']=7In [159]: df.iat[3,0]=7
at may enlarge the object in-place as above if the indexer is missing.
In [160]: df.at[dates[-1]+pd.Timedelta('1 day'),0]=7In [161]: dfOut[161]: A B C D E 02000-01-01 -0.282863 0.469112 -1.509059 -1.135632 NaN NaN2000-01-02 -0.173215 1.212112 0.119209 -1.044236 NaN NaN2000-01-03 -2.104569 -0.861849 -0.494929 1.071804 NaN NaN2000-01-04 7.000000 0.721555 -1.039575 0.271860 NaN NaN2000-01-05 0.567020 -0.424972 0.276232 -1.087401 NaN NaN2000-01-06 0.113648 -0.673690 -1.478427 0.524988 7.0 NaN2000-01-07 0.577046 0.404705 -1.715002 -1.039268 NaN NaN
Boolean indexing
Another common operation is the use of boolean vectors to filter the data.
The operators are: | for or, & for and, and ~ for not.
These must be grouped by using parentheses, since by default Python will
evaluate an expression such as df['A']>2&df['B']<3 as
df['A']>(2&df['B'])<3, while the desired evaluation order is
(df['A']>2)&(df['B']<3).
Using a boolean vector to index a Series works exactly as in a NumPy ndarray:
You may select rows from a DataFrame using a boolean vector the same length as
the DataFrame’s index (for example, something derived from one of the columns
of the DataFrame):
In [167]: df[df['A']>0]Out[167]: A B C D E 02000-01-04 7.000000 0.721555 -1.039575 0.271860 NaN NaN2000-01-05 0.567020 -0.424972 0.276232 -1.087401 NaN NaN2000-01-06 0.113648 -0.673690 -1.478427 0.524988 7.0 NaN2000-01-07 0.577046 0.404705 -1.715002 -1.039268 NaN NaN
List comprehensions and the map method of Series can also be used to produce
more complex criteria:
In [168]: df2=pd.DataFrame({'a':['one','one','two','three','two','one','six'], .....: 'b':['x','y','y','x','y',
In [173]: df2.loc[criterion&(df2['b']=='x'),'b':'c']Out[173]: b c3 x 0.361719
Indexing with isin
Consider the isin() method of Series, which returns a boolean
vector that is true wherever the Series elements exist in the passed list.
This allows you to select rows where one or more columns have values you want:
The same method is available for Index objects and is useful for the cases
when you don’t know which of the sought labels are in fact present:
In [183]: s[s.index.isin([2,4,6])]Out[183]: 4 02 2dtype: int64# compare it to the followingIn [184]: s.reindex([2,4,6])Out[184]: 2 2.04 0.06 NaNdtype: float64
In addition to that, MultiIndex allows selecting a separate level to use
in the membership check:
In [185]: s_mi=pd.Series(np.arange(6), .....: index=pd.MultiIndex.from_product([[0,1],['a','b','c']])) .....: In [186]: s_miOut[186]:
DataFrame also has an isin() method. When calling isin, pass a set of
values as either an array or dict. If values is an array, isin returns
a DataFrame of booleans that is the same shape as the original DataFrame, with True
wherever the element is in the sequence of values.
In [189]: df=pd.DataFrame({'vals':[1,2,3,4],'ids':['a','b','f','n'], .....: 'ids2':['a','n','c'
Oftentimes you’ll want to match certain values with certain columns.
Just make values a dict where the key is the column, and the value is
a list of items you want to check for.
Combine DataFrame’s isin with the any() and all() methods to
quickly select subsets of your data that meet a given criteria.
To select a row where each column meets its own criterion:
In [196]: values={'ids':['a','b'],'ids2':['a','c'],'vals':[1,3]}In [197]: row_mask=df.isin(values).all(1
Selecting values from a Series with a boolean vector generally returns a
subset of the data. To guarantee that selection output has the same shape as
the original data, you can use the where method in Series and DataFrame.
To return only the selected rows:
In [199]: s[s>0]Out[199]: 3 12 21 30 4dtype: int64
To return a Series of the same shape as the original:
In [200]: s.where(s>0)Out[200]: 4 NaN3 1.02 2.01 3.00 4.0dtype: float64
Selecting values from a DataFrame with a boolean criterion now also preserves
input data shape. where is used under the hood as the implementation.
The code below is equivalent to df.where(df<0).
In [201]: dates=pd.date_range('1/1/2000',periods=8)In [202]: df=pd.DataFrame(np.random.randn(8,4), .....: index=dates,columns=[
In addition, where takes an optional other argument for replacement of
values where the condition is False, in the returned copy.
In [204]: df.where(df<0,-df)Out[204]: A B C D2000-01-01 -2.104139 -1.309525 -0.485855 -0.2451662000-01-02 -0.352480 -0.390389 -1.192319 -1.6558242000-01-03 -0.864883 -0.299674 -0.227870 -0.2810592000-01-04 -0.846958 -1.222082 -0.600705 -1.2332032000-01-05 -0.669692 -0.605656 -1.169184 -0.3424162000-01-06 -0.868584 -0.948458 -2.297780 -0.6847182000-01-07 -2.670153 -0.114722 -0.168904 -0.0480482000-01-08 -0.801196 -1.392071 -0.048788 -0.808838
You may wish to set values based on some boolean criteria.
This can be done intuitively like so:
Furthermore, where aligns the input boolean condition (ndarray or DataFrame),
such that partial selection with setting is possible. This is analogous to
partial setting via .loc (but on the contents rather than the axis labels).
In [212]: df2=df.copy()In [213]: df2[df2[1:4]>0]=3In [214]: df2Out[214]: A B C D2000-01-01 -2.104139 -1.309525 0.485855 0.2451662000-01-02 -0.352480 3.000000 -1.192319 3.0000002000-01-03 -0.864883 3.000000 -0.227870 3.0000002000-01-04 3.000000 -1.222082 3.000000 -1.2332032000-01-05 0.669692 -0.605656 -1.169184 0.3424162000-01-06 0.868584 -0.948458 2.297780 -0.6847182000-01-07 -2.670153 -0.114722 0.168904 -0.048048
Where can also accept axis and level parameters to align the input when
performing the where.
In [215]: df2=df.copy()In [216]: df2.where(df2>0,df2['A'],axis='index')Out[216]: A B C D2000-01-01 -2.104139 -2.104139 0.485855 0.2451662000-01-02 -0.352480 0.390389 -0.352480 1.6558242000-01-03 -0.864883 0.299674 -0.864883 0.2810592000-01-04 0.846958 0.846958 0.600705 0.8469582000-01-05 0.669692 0.669692 0.669692 0.3424162000-01-06 0.868584 0.868584 2.297780 0.8685842000-01-07 -2.670153 -2.670153 0.168904 -2.670153
This is equivalent to (but faster than) the following.
In [217]: df2=df.copy()In [218]: df.apply(lambdax,y:x.where(x>0,y),y=df['A'])Out[218]: A B C D2000-01-01 -2.104139 -2.104139 0.485855 0.2451662000-01-02 -0.352480 0.390389 -0.352480 1.655824
where can accept a callable as condition and other arguments. The function must
be with one argument (the calling Series or DataFrame) and that returns valid output
as condition and other argument.
In [219]: df3=pd.DataFrame({'A':[1,2,3], .....: 'B':[4,5,6], .....: 'C':[7,8,9]}) .....:
Setting with enlargement conditionally using numpy()
An alternative to where() is to use numpy.where().
Combined with setting a new column, you can use it to enlarge a DataFrame where the
values are determined conditionally.
Consider you have two choices to choose from in the following DataFrame. And you want to
set a new column color to ‘green’ when the second column has ‘Z’. You can do the
following:
In [223]: df=pd.DataFrame({'col1':list('ABBC'),'col2':list('ZZXY')})In [224]: df['color']=np.where(df['col2']=='Z','green','red')In [225]: dfOut[225]: col1 col2 color0 A Z green1 B Z green2 B X red3 C Y red
If you have multiple conditions, you can use numpy.select() to achieve that. Say
corresponding to three conditions there are three choice of colors, with a fourth color
as a fallback, you can do the following.
In [226]: conditions=[ .....: (df['col2']=='Z')&(df['col1']=='A'), .....: (df['col2']=='Z')&(df['col1']
A use case for query() is when you have a collection of
DataFrame objects that have a subset of column names (or index
levels/names) in common. You can pass the same query to both frames without
having to specify which frame you’re interested in querying
In [264]: df=pd.DataFrame(np.random.randint(n,size=(n,3)),columns=list(
The in and notin operators
query() also supports special use of Python’s in and
notin comparison operators, providing a succinct syntax for calling the
isin method of a Series or DataFrame.
# get all rows where columns "a" and "b" have overlapping values
Special use of the == operator with list objects
Comparing a list of values to a column using ==/!= works similarly
to in/notin.
In [279]: df.query('b == ["a", "b", "c"]')Out[279]: a b c d
Boolean operators
You can negate boolean expressions with the word not or the ~ operator.
In [286]: df=pd.DataFrame(np.random.rand(n,3),columns=list('abc'))In [287]: df[
DataFrame.query() using numexpr is slightly faster than Python for
large frames.
You will only see the performance benefits of using the numexpr engine
with DataFrame.query() if your frame has more than approximately 100,000
rows.
This plot was created using a DataFrame with 3 columns each containing
floating point values generated using numpy.random.randn().
Duplicate data
If you want to identify and remove duplicate rows in a DataFrame, there are
two methods that will help: duplicated and drop_duplicates. Each
takes as an argument the columns to use to identify duplicated rows.
duplicated returns a boolean vector whose length is the number of rows, and which indicates whether a row is duplicated.
drop_duplicates removes duplicate rows.
By default, the first observed row of a duplicate set is considered unique, but
each method has a keep parameter to specify targets to be kept.
keep='first' (default): mark / drop duplicates except for the first occurrence.
keep='last': mark / drop duplicates except for the last occurrence.
keep=False: mark / drop all duplicates.
In [298]: df2=pd.DataFrame({'a':['one','one','two','two','two','three','four'], .....: 'b':['x','y','x','y','x'
Also, you can pass a list of columns to identify duplications.
In [306]: df2.duplicated(['a','b'])Out[306]: 0 False1 False2 False3 False4 True5 False6 Falsedtype: boolIn [307]: df2.drop_duplicates(['a','b'])Out[307]: a b c0 one x -1.0671371 one y 0.3095002 two x -0.2110563 two y -1.8420235 three x -1.9644756 four x 1.298329
To drop duplicates by index value, use Index.duplicated then perform slicing.
The same set of options are available for the keep parameter.
In [308]: df3=pd.DataFrame({'a':np.arange(6), .....: 'b':np.random.randn(6)}, .....: index=['a','a','b','c'
Each of Series or DataFrame have a get method which can return a
default value.
In [314]: s=pd.Series([1,2,3],index=['a','b','c'])In [315]: s.get('a')# equivalent to s['a']Out[315]: 1In [316]: s.get('x',default=-1)Out[316]: -1
Looking up values by index/column labels
Sometimes you want to extract a set of values given a sequence of row labels
and column labels, this can be achieved by pandas.factorize and NumPy indexing.
For instance:
In [317]: df=pd.DataFrame({'col':["A","A","B","B"], .....: 'A':[80,23,np.nan,22], .....: 'B':[80,55,76,67]}) .....: In [318]: dfOut[318]: col A B0 A 80.0 801 A 23.0 552 B NaN 763 B 22.0 67In [319]: idx,cols=pd.factorize(df['col'])In [320]: df.reindex(cols,axis=1).to_numpy()[np.arange(len(df)),idx]Out[320]: array([80., 23., 76., 67.])
Formerly this could be achieved with the dedicated DataFrame.lookup method
which was deprecated in version 1.2.0 and removed in version 2.0.0.
Index objects
The pandas Index class and its subclasses can be viewed as
implementing an ordered multiset. Duplicates are allowed.
Index also provides the infrastructure necessary for
lookups, data alignment, and reindexing. The easiest way to create an
Index directly is to pass a list or other sequence to
Index:
You can also pass a name to be stored in the index:
In [333]: index=pd.Index(['e','d','a','b'],name='something')In [334]: index.nameOut[334]: 'something'
The name, if set, will be shown in the console display:
In [335]: index=pd.Index(list(range(5)),name='rows')In [336]: columns=pd.Index(['A','B','C'],name='cols')In [337]: df=pd
Setting metadata
Indexes are “mostly immutable”, but it is possible to set and change their
name attribute. You can use the rename, set_names to set these attributes
directly, and they default to returning a copy.
The two main operations are union and intersection.
Difference is provided via the .difference() method.
In [350]: a=pd.Index(['c','b','a'])In [351]: b=pd.Index(['c'
Missing values
Index.fillna fills missing values with specified scalar value.
In [359]: idx1=pd.Index([1,np.nan
Set / reset index
Occasionally you will load or create a data set into a DataFrame and want to
add an index after you’ve already done so. There are a couple of different
ways.
Set an index
DataFrame has a set_index() method which takes a column name
(for a regular Index) or a list of column names (for a MultiIndex).
To create a new, re-indexed DataFrame:
In [365]: data=pd.DataFrame({'a':['bar','bar','foo','foo'], .....: 'b':['one','two','one','two'], .....: 'c':['z','y','x','w'], .....: 'd':[1.,2.,3,4]}) .....: In [366]: dataOut[366]: a b c d0 bar one z 1.01 bar two y 2.02 foo one x 3.03 foo two w 4.0In [367]: indexed1=data.set_index('c')In [368]: indexed1Out[368]: a b dc z bar one 1.0y bar two 2.0x foo one 3.0w foo two 4.0In [369]: indexed2=data.set_index(['a','b'])In [370]: indexed2Out[370]: c da b bar one z 1.0 two y 2.0foo one x 3.0 two w 4.0
The append keyword option allow you to keep the existing index and append
the given columns to a MultiIndex:
In [371]: frame=data.set_index('c',drop=False)In [372]: frame=frame.set_index(['a','b'],append=True)In [373]: frameOut[373]: c dc a b z bar one z 1.0y bar two y 2.0
Other options in set_index allow you not drop the index columns.
In [374]: data.set_index('c',drop=False)Out[374]: a b c dc z bar one z 1.0y bar two y 2.0x foo one x 3.0w foo two w 4.0
Reset the index
As a convenience, there is a new function on DataFrame called
reset_index() which transfers the index values into the
DataFrame’s columns and sets a simple integer index.
This is the inverse operation of set_index().
In [375]: dataOut[375]: a b c d0 bar one z 1.01 bar two y 2.02 foo one x 3.03 foo two w 4.0In [376]: data.reset_index()Out[376]: index a b c d
Adding an ad hoc index
You can assign a custom index to the index attribute:
In [379]: df_idx=pd.DataFrame(range(4))In [380]: df_idx.index=pd.Index([10,20,30,40],name=
Returning a view versus a copy
When setting values in a pandas object, care must be taken to avoid what is called
chainedindexing. Here is an example.
These both yield the same results, so which should you use? It is instructive to understand the order
of operations on these and why method 2 (.loc) is much preferred over method 1 (chained []).
dfmi['one'] selects the first level of the columns and returns a DataFrame that is singly-indexed.
Then another Python operation dfmi_with_one['second'] selects the series indexed by 'second'.
This is indicated by the variable dfmi_with_one because pandas sees these operations as separate events.
e.g. separate calls to __getitem__, so it has to treat them as linear operations, they happen one after another.
Contrast this to df.loc[:,('one','second')] which passes a nested tuple of (slice(None),('one','second')) to a single call to
__getitem__. This allows pandas to deal with this as a single entity. Furthermore this order of operations can be significantly
faster, and allows one to index both axes if so desired.
Why does assignment fail when using chained indexing?
The problem in the previous section is just a performance issue. What’s up with
the SettingWithCopy warning? We don’t usually throw warnings around when
you do something that might cost a few extra milliseconds!
But it turns out that assigning to the product of chained indexing has
inherently unpredictable results. To see this, think about how the Python
interpreter executes this code:
Evaluation order matters
When you use chained indexing, the order and type of the indexing operation
partially determine whether the result is a slice into the original object, or
a copy of the slice.
pandas has the SettingWithCopyWarning because assigning to a copy of a
slice is frequently not intentional, but a mistake caused by chained indexing
returning a copy where a slice was expected.
NA
False
A callable function with one argument (the calling Series or DataFrame) and
that returns valid output for indexing (one of the above).
A tuple of row (and column) indices whose elements are one of the
above inputs.
If at least one of the two is absent, but the index is sorted, and can be
compared against start and stop labels, then slicing will still work as
expected, by selecting labels which rank between the two:
However, if at least one of the two is absent and the index is not sorted, an
error will be raised (since doing otherwise would be computationally expensive,
as well as potentially ambiguous for mixed type indexes). For instance, in the
above example, s.loc[1:6] would raise KeyError.
In [70]: s=pd.Series(list('abcdef'),index=[0,3,2,5,4,2])In [71]: s.loc[3:5]Out[71]: 3 b2 c5 ddtype: object
Also, if the index has duplicate labels and either the start or the stop label is duplicated,
an error will be raised. For instance, in the above example, s.loc[2:5] would raise a KeyError.
For more information about duplicate labels, see
Duplicate Labels.
s1
.
iloc
[:
3
]
Out[74]:
0 0.695775
2 0.341734
4 0.959726
dtype: float64
In [75]:
s1
.
iloc
[
3
]
Out[75]:
-1.110336102891167
,
8
,
2
)))
....:
In [79]:
df1
Out[79]:
0 2 4 6
0 0.149748 -0.732339 0.687738 0.176444
2 0.403310 -0.154951 0.301624 -2.179861
4 -1.369849 -0.954208 1.462696 -1.743161
6 -0.826591 -0.345352 1.314232 0.690579
8 0.995761 2.396780 0.014871 3.357427
10 -0.317441 -1.236269 0.896171 -0.487602
(
x
)
In [92]:
s
Out[92]:
0 a
1 b
2 c
3 d
4 e
5 f
dtype: object
In [93]:
s
.
iloc
[
4
:
10
]
Out[93]:
4 e
5 f
dtype: object
In [94]:
s
.
iloc
[
8
:
10
]
Out[94]:
Series([], dtype: object)
[:,
2
:
3
]
Out[97]:
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3, 4]
In [98]:
dfl
.
iloc
[:,
1
:
3
]
Out[98]:
B
0 -2.182937
1 0.084844
2 1.519970
3 0.600178
4 0.132885
In [99]:
dfl
.
iloc
[
4
:
6
]
Out[99]:
A B
4 0.27423 0.132885
)
1716
except
IndexError
as
err
:
1717
# re-raise with different error message, e.g. test_getitem_ndarray_3d
Alternatively, if you want to select only valid keys, the following is idiomatic and efficient; it is guaranteed to preserve the dtype of the selection.
In [118]: labels=[1,2,3]In [119]: s.loc[s.index.intersection(labels)]Out[119]: 1 22 3dtype: int64
Having a duplicated index will raise for a .reindex():
In [120]: s=pd.Series(np.arange(4),index=['a','a','b','c'])In [121]: labels=['c','d']In [122]: s.reindex(labels)---------------------------------------------------------------------------ValueErrorTraceback (most recent call last)
CellIn[122],line1----> 1s.reindex(labels)File ~/work/pandas/pandas/pandas/core/series.py:5172, in Series.reindex(self, index, axis, method, copy, level, fill_value, limit, tolerance)5155@doc(5156NDFrame.reindex,# type: ignore[has-type]5157klass=_shared_doc_kwargs["klass"],(...)5170tolerance=None,5171)->Series:-> 5172returnsuper().reindex(5173index=index,5174method=method,5175copy=copy,5176level=level,5177fill_value=fill_value,5178limit=limit,5179tolerance=tolerance,5180)File ~/work/pandas/pandas/pandas/core/generic.py:5632, in NDFrame.reindex(self, labels, index, columns, axis, method, copy, level, fill_value, limit, tolerance)5629returnself._reindex_multi(axes,copy,fill_value)5631# perform the reindex on the axes-> 5632returnself._reindex_axes(5633axes,level,limit,tolerance,method,fill_value,copy5634).__finalize__(self,method="reindex")File ~/work/pandas/pandas/pandas/core/generic.py:5655, in NDFrame._reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)5652continue5654ax=self._get_axis(a)-> 5655new_index,indexer=ax.reindex(5656labels,level=level,limit=limit,tolerance=tolerance,method=method5657)5659axis=self._get_axis_number(a)5660obj=obj._reindex_with_indexers(5661{axis:[new_index,indexer]},5662fill_value=fill_value,5663copy=copy,5664allow_dups=False,5665)File ~/work/pandas/pandas/pandas/core/indexes/base.py:4436, in Index.reindex(self, target, method, level, limit, tolerance)4433raiseValueError("cannot handle a non-unique multi-index!")4434elifnotself.is_unique:4435# GH#42568-> 4436raiseValueError("cannot reindex on an axis with duplicate labels")4437else:4438indexer,_=self.get_indexer_non_unique(target)ValueError: cannot reindex on an axis with duplicate labels
Generally, you can intersect the desired labels with the current
axis, and then reindex.
In [123]: s.loc[s.index.intersection(labels)].reindex(labels)Out[123]: c 3.0d NaNdtype: float64
However, this would still raise if your resulting index is duplicated.
In [124]: labels=['a','d']In [125]: s.loc[s.index.intersection(labels)].reindex(labels)---------------------------------------------------------------------------ValueErrorTraceback (most recent call last)
CellIn[125],line1----> 1s.loc[s.index.intersection(labels)].reindex(labels)File ~/work/pandas/pandas/pandas/core/series.py:5172, in Series.reindex(self, index, axis, method, copy, level, fill_value, limit, tolerance)5155@doc(5156NDFrame.reindex,# type: ignore[has-type]5157klass=_shared_doc_kwargs["klass"],(...)5170tolerance=None,5171)->Series:-> 5172returnsuper().reindex(5173index=index,5174method=method,5175copy=copy,5176level=level,5177fill_value=fill_value,5178limit=limit,5179tolerance=tolerance,5180)File ~/work/pandas/pandas/pandas/core/generic.py:5632, in NDFrame.reindex(self, labels, index, columns, axis, method, copy, level, fill_value, limit, tolerance)5629returnself._reindex_multi(axes,copy,fill_value)5631# perform the reindex on the axes-> 5632returnself._reindex_axes(5633axes,level,limit,tolerance,method,fill_value,copy5634).__finalize__(self,method="reindex")File ~/work/pandas/pandas/pandas/core/generic.py:5655, in NDFrame._reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)5652continue5654ax=self._get_axis(a)-> 5655new_index,indexer=ax.reindex(5656labels,level=level,limit=limit,tolerance=tolerance,method=method5657)5659axis=self._get_axis_number(a)5660obj=obj._reindex_with_indexers(5661{axis:[new_index,indexer]},5662fill_value=fill_value,5663copy=copy,5664allow_dups=False,5665)File ~/work/pandas/pandas/pandas/core/indexes/base.py:4436, in Index.reindex(self, target, method, level, limit, tolerance)4433raiseValueError("cannot handle a non-unique multi-index!")4434elifnotself.is_unique:4435# GH#42568-> 4436raiseValueError("cannot reindex on an axis with duplicate labels")4437else:4438indexer,_=self.get_indexer_non_unique(target)ValueError: cannot reindex on an axis with duplicate labels
3 3
2 2
4 4
dtype: int64
# With replacement:
In [132]:
s
.
sample
(
n
=
6
,
replace
=
True
)
Out[132]:
0 0
4 4
3 3
2 2
4 4
4 4
dtype: int64
sample
(
n
=
3
,
weights
=
example_weights
)
Out[135]:
5 5
4 4
3 3
dtype: int64
# Weights will be re-normalized automatically
In [136]:
example_weights2
=
[
0.5
,
0
,
0
,
0
,
0
,
0
]
In [137]:
s
.
sample
(
n
=
1
,
weights
=
example_weights2
)
Out[137]:
0 0
dtype: int64
=
3
,
weights
=
'weight_column'
)
Out[139]:
col1 weight_column
1 8 0.4
0 9 0.5
2 7 0.1
Out[141]:
col1
0 1
1 2
2 3
2
)
Out[143]:
col1 col2
2 3 4
1 2 3
In [144]:
df4
.
sample
(
n
=
2
,
random_state
=
2
)
Out[144]:
col1 col2
2 3 4
1 2 3
dfi
.
loc
[:,
'C'
]
=
dfi
.
loc
[:,
'A'
]
In [152]:
dfi
Out[152]:
A B C
0 0 1 0
1 2 3 2
2 4 5 4
2000-01-08 -1.157892 -0.370647 -1.344312 0.844885 NaN NaN
2000-01-09 NaN NaN NaN NaN NaN 7.0
In [165]:
s
[(
s
<
-
1
)
|
(
s
>
0.5
)]
Out[165]:
0 -3
1 -2
4 1
5 2
6 3
dtype: int64
In [166]:
s
[
~
(
s
<
0
)]
Out[166]:
3 0
4 1
5 2
6 3
dtype: int64
'x'
,
'x'
],
.....:
'c'
:
np
.
random
.
randn
(
7
)})
.....:
# only want 'two' or 'three'
In [169]:
criterion
=
df2
[
'a'
]
.
map
(
lambda
x
:
x
.
startswith
(
't'
))
In [170]:
df2
[
criterion
]
Out[170]:
a b c
2 two y 0.041290
3 three x 0.361719
4 two y -0.238075
# equivalent but slower
In [171]:
df2
[[
x
.
startswith
(
't'
)
for
x
in
df2
[
'a'
]]]
Out[171]:
a b c
2 two y 0.041290
3 three x 0.361719
4 two y -0.238075
# Multiple criteria
In [172]:
df2
[
criterion
&
(
df2
[
'b'
]
==
'x'
)]
Out[172]:
a b c
3 three x 0.361719
],
[
3
,
4
],
[
5
,
6
]],
.....:
index
=
list
(
'abc'
),
.....:
columns
=
[
'A'
,
'B'
])
.....:
In [175]:
s
=
(
df
[
'A'
]
>
2
)
In [176]:
s
Out[176]:
a False
b True
c True
Name: A, dtype: bool
In [177]:
df
.
loc
[
s
,
'B'
]
Out[177]:
b 4
c 6
Name: B, dtype: int64
In [178]:
df
.
iloc
[
s
.
values
,
1
]
Out[178]:
b 4
c 6
Name: B, dtype: int64
0 a 0
b 1
c 2
1 a 3
b 4
c 5
dtype: int64
In [187]:
s_mi
.
iloc
[
s_mi
.
index
.
isin
([(
1
,
'a'
),
(
2
,
'b'
),
(
0
,
'c'
)])]
Out[187]:
0 c 2
1 a 3
dtype: int64
In [188]:
s_mi
.
iloc
[
s_mi
.
index
.
isin
([
'a'
,
'c'
,
'e'
],
level
=
1
)]
Out[188]:
0 a 0
c 2
1 a 3
c 5
dtype: int64
,
'n'
]})
.....:
In [190]:
values
=
[
'a'
,
'b'
,
1
,
3
]
In [191]:
df
.
isin
(
values
)
Out[191]:
vals ids ids2
0 True True True
1 False True False
2 True False False
3 False False False
)
In [198]:
df
[
row_mask
]
Out[198]:
vals ids ids2
0 1 a a
'A'
,
'B'
,
'C'
,
'D'
])
.....:
In [203]:
df
[
df
<
0
]
Out[203]:
A B C D
2000-01-01 -2.104139 -1.309525 NaN NaN
2000-01-02 -0.352480 NaN -1.192319 NaN
2000-01-03 -0.864883 NaN -0.227870 NaN
2000-01-04 NaN -1.222082 NaN -1.233203
2000-01-05 NaN -0.605656 -1.169184 NaN
2000-01-06 NaN -0.948458 NaN -0.684718
2000-01-07 -2.670153 -0.114722 NaN -0.048048
2000-01-08 NaN NaN -0.048788 -0.808838
<
0
]
=
0
In [210]:
df2
Out[210]:
A B C D
2000-01-01 0.000000 0.000000 0.485855 0.245166
2000-01-02 0.000000 0.390389 0.000000 1.655824
2000-01-03 0.000000 0.299674 0.000000 0.281059
2000-01-04 0.846958 0.000000 0.600705 0.000000
2000-01-05 0.669692 0.000000 0.000000 0.342416
2000-01-06 0.868584 0.000000 2.297780 0.000000
2000-01-07 0.000000 0.000000 0.168904 0.000000
2000-01-08 0.801196 1.392071 0.000000 0.000000
,
-
df
)
==
np
.
where
(
df
<
0
,
df
,
-
df
)
Out[211]:
A B C D
2000-01-01 True True True True
2000-01-02 True True True True
2000-01-03 True True True True
2000-01-04 True True True True
2000-01-05 True True True True
2000-01-06 True True True True
2000-01-07 True True True True
2000-01-08 True True True True
2000-01-08 0.801196 1.392071 -0.048788 -0.808838
2000-01-08 0.801196 1.392071 0.801196 0.801196
2000-01-03 -0.864883 0.299674 -0.864883 0.281059
2000-01-04 0.846958 0.846958 0.600705 0.846958
2000-01-05 0.669692 0.669692 0.669692 0.342416
2000-01-06 0.868584 0.868584 2.297780 0.868584
2000-01-07 -2.670153 -2.670153 0.168904 -2.670153
2000-01-08 0.801196 1.392071 0.801196 0.801196
In [220]:
df3
.
where
(
lambda
x
:
x
>
4
,
lambda
x
:
x
+
10
)
Out[220]:
A B C
0 11 14 7
1 12 5 8
2 13 6 9
>=
0
)
Out[222]:
A B C D
2000-01-01 -2.104139 -1.309525 NaN NaN
2000-01-02 -0.352480 NaN -1.192319 NaN
2000-01-03 -0.864883 NaN -0.227870 NaN
2000-01-04 NaN -1.222082 NaN -1.233203
2000-01-05 NaN -0.605656 -1.169184 NaN
2000-01-06 NaN -0.948458 NaN -0.684718
2000-01-07 -2.670153 -0.114722 NaN -0.048048
2000-01-08 NaN NaN -0.048788 -0.808838
==
'B'
),
.....:
(
df
[
'col1'
]
==
'B'
)
.....:
]
.....:
In [227]:
choices
=
[
'yellow'
,
'blue'
,
'purple'
]
In [228]:
df
[
'color'
]
=
np
.
select
(
conditions
,
choices
,
default
=
'black'
)
In [229]:
df
Out[229]:
col1 col2 color
0 A Z yellow
1 B Z blue
2 B X purple
3 C Y black
In [237]:
df
Out[237]:
b c
a
0 0 4
1 0 1
2 3 4
3 4 3
4 1 4
5 0 3
6 0 1
7 3 4
8 2 3
9 1 1
In [238]:
df
.
query
(
'a < b and b < c'
)
Out[238]:
b c
a
2 3 4
5 0 1
6 2 5
7 0 1
8 6 0
9 7 9
In [241]:
df
.
query
(
'index < b < c'
)
Out[241]:
b c
2 5 6
'a'
In [244]:
df
.
query
(
'a > 2'
)
# uses the column 'a', not the index
Out[244]:
a
a
1 3
3 3
You can still use the index in a query expression by using the special
identifier ‘index’:
In [245]: df.query('index > 2')Out[245]: aa 3 34 2
If for some reason you have a column named index, then you can refer to
the index as ilevel_0 as well, but at this point you should consider
renaming your columns to something less ambiguous.
If the levels of the MultiIndex are unnamed, you can refer to them using
special names:
In [255]: df.index.names=[None,None]In [256]: dfOut[256]: 0 1red ham 0.194889 -0.381994 ham 0.318587 2.089075 eggs -0.728293 -0.090255green eggs -0.748199 1.318931 eggs -2.029766 0.792652 ham 0.461007 -0.542749 ham -0.305384 -0.479195 eggs 0.095031 -0.270099 eggs -0.707140 -0.773882 eggs 0.229453 0.304418In [257]: df.query('ilevel_0 == "red"')Out[257]: 0 1red ham 0.194889 -0.381994 ham 0.318587 2.089075 eggs -0.728293 -0.090255
The convention is ilevel_0, which means “index level 0” for the 0th level
of the index.
.
rand
(
n
,
3
),
columns
=
list
(
'abc'
))
In [259]:
df
Out[259]:
a b c
0 0.224283 0.736107 0.139168
1 0.302827 0.657803 0.713897
2 0.611185 0.136624 0.984960
3 0.195246 0.123436 0.627712
4 0.618673 0.371660 0.047902
5 0.480088 0.062993 0.185760
6 0.568018 0.483467 0.445289
7 0.309040 0.274580 0.587101
8 0.258993 0.477769 0.370255
9 0.550459 0.840870 0.304611
In [260]:
df2
=
pd
.
DataFrame
(
np
.
random
.
rand
(
n
+
2
,
3
),
columns
=
df
.
columns
)
In [261]:
df2
Out[261]:
a b c
0 0.357579 0.229800 0.596001
1 0.309059 0.957923 0.965663
2 0.123102 0.336914 0.318616
3 0.526506 0.323321 0.860813
4 0.518736 0.486514 0.384724
5 0.190804 0.505723 0.614533
6 0.891939 0.623977 0.676639
7 0.480559 0.378528 0.460858
8 0.420223 0.136404 0.141295
9 0.732206 0.419540 0.604675
10 0.604466 0.848974 0.896165
11 0.589168 0.920046 0.732716
In [262]:
expr
=
'0.0 <= a <= c <= 0.5'
In [263]:
map
(
lambda
frame
:
frame
.
query
(
expr
),
[
df
,
df2
])
Out[263]:
<map at 0x7f8c5c2797b0>
'abc'
))
In [265]:
df
Out[265]:
a b c
0 7 8 9
1 1 0 7
2 2 7 2
3 6 2 2
4 2 6 3
5 3 8 2
6 1 7 2
7 5 1 5
8 9 8 0
9 1 5 0
In [266]:
df
.
query
(
'(a < b) & (b < c)'
)
Out[266]:
a b c
0 7 8 9
In [267]:
df
[(
df
[
'a'
]
<
df
[
'b'
])
&
(
df
[
'b'
]
<
df
[
'c'
])]
Out[267]:
a b c
0 7 8 9
Slightly nicer by removing the parentheses (comparison operators bind tighter
than & and |):
In [268]: df.query('a < b & b < c')Out[268]: a b c0 7 8 9
Use English instead of symbols:
In [269]: df.query('a < b and b < c')Out[269]: a b c0 7 8 9
Pretty close to how you might write it on paper:
In [270]: df.query('a < b < c')Out[270]: a b c0 7 8 9
In [271]:
df
=
pd
.
DataFrame
({
'a'
:
list
(
'aabbccddeeff'
),
'b'
:
list
(
'aaaabbbbcccc'
),
.....:
'c'
:
np
.
random
.
randint
(
5
,
size
=
12
),
.....:
'd'
:
np
.
random
.
randint
(
9
,
size
=
12
)})
.....:
In [272]:
df
Out[272]:
a b c d
0 a a 2 6
1 a a 4 7
2 b a 1 6
3 b a 2 1
4 c b 3 6
5 c b 0 2
6 d b 3 3
7 d b 2 1
8 e c 4 3
9 e c 2 0
10 f c 0 6
11 f c 1 2
In [273]:
df
.
query
(
'a in b'
)
Out[273]:
a b c d
0 a a 2 6
1 a a 4 7
2 b a 1 6
3 b a 2 1
4 c b 3 6
5 c b 0 2
# How you'd do it in pure Python
In [274]:
df
[
df
[
'a'
]
.
isin
(
df
[
'b'
])]
Out[274]:
a b c d
0 a a 2 6
1 a a 4 7
2 b a 1 6
3 b a 2 1
4 c b 3 6
5 c b 0 2
In [275]:
df
.
query
(
'a not in b'
)
Out[275]:
a b c d
6 d b 3 3
7 d b 2 1
8 e c 4 3
9 e c 2 0
10 f c 0 6
11 f c 1 2
# pure Python
In [276]:
df
[
~
df
[
'a'
]
.
isin
(
df
[
'b'
])]
Out[276]:
a b c d
6 d b 3 3
7 d b 2 1
8 e c 4 3
9 e c 2 0
10 f c 0 6
11 f c 1 2
You can combine this with other expressions for very succinct queries:
# rows where cols a and b have overlapping values# and col c's values are less than col d'sIn [277]: df.query('a in b and c < d')Out[277]: a b c d0 a a 2 61 a a 4 72 b a 1 64 c b 3 65 c b 0 2# pure PythonIn [278]: df[df['b'].isin(df['a'])&(df['c']<df['d'])]Out[278]: a b c d0 a a 2 61 a a 4 72 b a 1 64 c b 3 65 c b 0 210 f c 0 611 f c 1 2
0 a a 2 6
1 a a 4 7
2 b a 1 6
3 b a 2 1
4 c b 3 6
5 c b 0 2
6 d b 3 3
7 d b 2 1
8 e c 4 3
9 e c 2 0
10 f c 0 6
11 f c 1 2
# pure Python
In [280]:
df
[
df
[
'b'
]
.
isin
([
"a"
,
"b"
,
"c"
])]
Out[280]:
a b c d
0 a a 2 6
1 a a 4 7
2 b a 1 6
3 b a 2 1
4 c b 3 6
5 c b 0 2
6 d b 3 3
7 d b 2 1
8 e c 4 3
9 e c 2 0
10 f c 0 6
11 f c 1 2
In [281]:
df
.
query
(
'c == [1, 2]'
)
Out[281]:
a b c d
0 a a 2 6
2 b a 1 6
3 b a 2 1
7 d b 2 1
9 e c 2 0
11 f c 1 2
In [282]:
df
.
query
(
'c != [1, 2]'
)
Out[282]:
a b c d
1 a a 4 7
4 c b 3 6
5 c b 0 2
6 d b 3 3
8 e c 4 3
10 f c 0 6
# using in/not in
In [283]:
df
.
query
(
'[1, 2] in c'
)
Out[283]:
a b c d
0 a a 2 6
2 b a 1 6
3 b a 2 1
7 d b 2 1
9 e c 2 0
11 f c 1 2
In [284]:
df
.
query
(
'[1, 2] not in c'
)
Out[284]:
a b c d
1 a a 4 7
4 c b 3 6
5 c b 0 2
6 d b 3 3
8 e c 4 3
10 f c 0 6
# pure Python
In [285]:
df
[
df
[
'c'
]
.
isin
([
1
,
2
])]
Out[285]:
a b c d
0 a a 2 6
2 b a 1 6
3 b a 2 1
7 d b 2 1
9 e c 2 0
11 f c 1 2
'bools'
]
=
np
.
random
.
rand
(
len
(
df
))
>
0.5
In [288]:
df
.
query
(
'~bools'
)
Out[288]:
a b c bools
2 0.697753 0.212799 0.329209 False
7 0.275396 0.691034 0.826619 False
8 0.190649 0.558748 0.262467 False
In [289]:
df
.
query
(
'not bools'
)
Out[289]:
a b c bools
2 0.697753 0.212799 0.329209 False
7 0.275396 0.691034 0.826619 False
8 0.190649 0.558748 0.262467 False
In [290]:
df
.
query
(
'not bools'
)
==
df
[
~
df
[
'bools'
]]
Out[290]:
a b c bools
2 True True True True
7 True True True True
8 True True True True
Of course, expressions can be arbitrarily complex too:
# short query syntaxIn [291]: shorter=df.query('a < b < c and (not bools) or bools > 2')# equivalent in pure PythonIn [292]: longer=df[(df['a']<df['b']) .....: &(df['b']<df['c']) .....: &(~df['bools']) .....: |(df['bools']>2)] .....: In [293]: shorterOut[293]: a b c bools7 0.275396 0.691034 0.826619 FalseIn [294]: longerOut[294]: a b c bools7 0.275396 0.691034 0.826619 FalseIn [295]: shorter==longerOut[295]: a b c bools7 True True True True
In [296]: df=pd.DataFrame(np.random.randn(8,4), .....: index=dates,columns=['A','B','C','D']) .....: In [297]: df2=df.copy()
,
'x'
,
'x'
],
.....:
'c'
:
np
.
random
.
randn
(
7
)})
.....:
In [299]:
df2
Out[299]:
a b c
0 one x -1.067137
1 one y 0.309500
2 two x -0.211056
3 two y -1.842023
4 two x -0.390820
5 three x -1.964475
6 four x 1.298329
In [300]:
df2
.
duplicated
(
'a'
)
Out[300]:
0 False
1 True
2 False
3 True
4 True
5 False
6 False
dtype: bool
In [301]:
df2
.
duplicated
(
'a'
,
keep
=
'last'
)
Out[301]:
0 True
1 False
2 True
3 True
4 False
5 False
6 False
dtype: bool
In [302]:
df2
.
duplicated
(
'a'
,
keep
=
False
)
Out[302]:
0 True
1 True
2 True
3 True
4 True
5 False
6 False
dtype: bool
In [303]:
df2
.
drop_duplicates
(
'a'
)
Out[303]:
a b c
0 one x -1.067137
2 two x -0.211056
5 three x -1.964475
6 four x 1.298329
In [304]:
df2
.
drop_duplicates
(
'a'
,
keep
=
'last'
)
Out[304]:
a b c
1 one y 0.309500
4 two x -0.390820
5 three x -1.964475
6 four x 1.298329
In [305]:
df2
.
drop_duplicates
(
'a'
,
keep
=
False
)
Out[305]:
a b c
5 three x -1.964475
6 four x 1.298329
,
'b'
,
'a'
])
.....:
In [309]:
df3
Out[309]:
a b
a 0 1.440455
a 1 2.456086
b 2 1.038402
c 3 -0.894409
b 4 0.683536
a 5 3.082764
In [310]:
df3
.
index
.
duplicated
()
Out[310]:
array([False, True, False, False, True, True])
In [311]:
df3
[
~
df3
.
index
.
duplicated
()]
Out[311]:
a b
a 0 1.440455
b 2 1.038402
c 3 -0.894409
In [312]:
df3
[
~
df3
.
index
.
duplicated
(
keep
=
'last'
)]
Out[312]:
a b
c 3 -0.894409
b 4 0.683536
a 5 3.082764
In [313]:
df3
[
~
df3
.
index
.
duplicated
(
keep
=
False
)]
Out[313]:
a b
c 3 -0.894409
=
"int8"
)
In [330]:
index
Out[330]:
Index([1, 5, 12], dtype='int8')
In [331]:
index
=
pd
.
Index
([
1
,
5
,
12
],
dtype
=
"float32"
)
In [332]:
index
Out[332]:
Index([1.0, 5.0, 12.0], dtype='float32')
.
DataFrame
(
np
.
random
.
randn
(
5
,
3
),
index
=
index
,
columns
=
columns
)
In [338]:
df
Out[338]:
cols A B C
rows
0 1.295989 -1.051694 1.340429
1 -2.366110 0.428241 0.387275
2 0.433306 0.929548 0.278094
3 2.154730 -0.315628 0.264223
4 1.126818 1.132290 -0.353310
In [339]:
df
[
'A'
]
Out[339]:
rows
0 1.295989
1 -2.366110
2 0.433306
3 2.154730
4 1.126818
Name: A, dtype: float64
rename
(
"apple"
)
Out[341]:
Index([1, 2, 3], dtype='int64', name='apple')
In [342]:
ind
Out[342]:
Index([1, 2, 3], dtype='int64')
In [343]:
ind
=
ind
.
set_names
([
"apple"
])
In [344]:
ind
.
name
=
"bob"
In [345]:
ind
Out[345]:
Index([1, 2, 3], dtype='int64', name='bob')
set_names, set_levels, and set_codes also take an optional
level argument
Also available is the symmetric_difference operation, which returns elements
that appear in either idx1 or idx2, but not in both. This is
equivalent to the Index created by idx1.difference(idx2).union(idx2.difference(idx1)),
with duplicates dropped.
In [353]: idx1=pd.Index([1,2,3,4])In [354]: idx2=pd.Index([2,3,4,5])In [355]: idx1.symmetric_difference(idx2)Out[355]: Index([1, 5], dtype='int64')
When performing Index.union() between indexes with different dtypes, the indexes
must be cast to a common dtype. Typically, though not always, this is object dtype. The
exception is when performing a union between integer and float data. In this case, the
integer values are converted to float
The output is more similar to a SQL table or a record array. The names for the
columns derived from the index are the ones stored in the names attribute.
You can use the level keyword to remove only a portion of the index:
In [377]: frameOut[377]: c dc a b z bar one z 1.0y bar two y 2.0x foo one x 3.0w foo two w 4.0In [378]: frame.reset_index(level=1)Out[378]: a c dc b z one bar z 1.0y two bar y 2.0x one foo x 3.0w two foo w 4.0
reset_index takes an optional parameter drop which if true simply
discards the index, instead of putting index values in the DataFrame’s columns.
See that __getitem__ in there? Outside of simple cases, it’s very hard to
predict whether it will return a view or a copy (it depends on the memory layout
of the array, about which pandas makes no guarantees), and therefore whether
the __setitem__ will modify dfmi or a temporary object that gets thrown
out immediately afterward. That’s what SettingWithCopy is warning you
about!
Sometimes a SettingWithCopy warning will arise at times when there’s no
obvious chained indexing going on. These are the bugs that
SettingWithCopy is designed to catch! pandas is probably trying to warn you
that you’ve done this:
defdo_something(df):foo=df[['bar','baz']]# Is foo a view? A copy? Nobody knows!# ... many lines here ...# We don't know whether this will modify df or not!foo['quux']=valuereturnfoo
Yikes!
If you would like pandas to be more or less trusting about assignment to a
chained indexing expression, you can set the optionmode.chained_assignment to one of these values:
'warn', the default, means a SettingWithCopyWarning is printed.
'raise' means pandas will raise a SettingWithCopyError
you have to deal with.
None will suppress the warnings entirely.
In [386]: dfb=pd.DataFrame({'a':['one','one','two', .....: 'three','two','one','six'], .....: 'c':np.arange(7)}) .....: # This will show the SettingWithCopyWarning# but the frame values will be setIn [387]: dfb['c'][dfb['a'].str.startswith('o')]=42
This however is operating on a copy and will not work.
In [388]: withpd.option_context('mode.chained_assignment','warn'): .....: dfb[dfb['a'].str.startswith('o')]['c']=42 .....:
A chained assignment can also crop up in setting in a mixed dtype frame.
The following is the recommended access method using .loc for multiple items (using mask) and a single item using a fixed index:
In [389]: dfc=pd.DataFrame({'a':['one','one','two', .....: 'three','two','one','six'], .....: 'c':np.arange(7)}) .....: In [390]: dfd=dfc.copy()# Setting multiple items using a maskIn [391]: mask=dfd['a'].str.startswith('o')In [392]: dfd.loc[mask,'c']=42In [393]: dfdOut[393]: a c0 one 421 one 422 two 23 three 34 two 45 one 426 six 6# Setting a single itemIn [394]: dfd=dfc.copy()In [395]: dfd.loc[2,'a']=11In [396]: dfdOut[396]: a c0 one 01 one 12 11 23 three 34 two 45 one 56 six 6
The following can work at times, but it is not guaranteed to, and therefore should be avoided:
In [397]: dfd=dfc.copy()In [398]: dfd['a'][2]=111In [399]: dfdOut[399]: a c0 one 01 one 12 111 23 three 34 two 45 one 56 six 6
Last, the subsequent example will not work at all, and so should be avoided:
In [400]: withpd.option_context('mode.chained_assignment','raise'): .....: dfd.loc[0]['a']=1111 .....: ---------------------------------------------------------------------------SettingWithCopyErrorTraceback (most recent call last)
<ipython-input-400-32ce785aaa5b> in ?()1withpd.option_context('mode.chained_assignment','raise'):----> 2dfd.loc[0]['a']=1111~/work/pandas/pandas/pandas/core/series.py in ?(self, key, value)1296)12971298check_dict_or_set_indexers(key)1299key=com.apply_if_callable(key,self)-> 1300cacher_needs_updating=self._check_is_chained_assignment_possible()13011302ifkeyisEllipsis:1303key=slice(None)~/work/pandas/pandas/pandas/core/series.py in ?(self)1501ref=self._get_cacher()1502ifrefisnotNoneandref._is_mixed_type:1503self._check_setitem_copy(t="referent",force=True)1504returnTrue-> 1505returnsuper()._check_is_chained_assignment_possible()~/work/pandas/pandas/pandas/core/generic.py in ?(self)4417single-dtypemeaningthatthecachershouldbeupdatedfollowing4418setting.4419"""4420 if self._is_copy:-> 4421 self._check_setitem_copy(t="referent")4422 return False~/work/pandas/pandas/pandas/core/generic.py in ?(self, t, force)4491 "indexing.html#returning-a-view-versus-a-copy"4492 )44934494 if value == "raise":-> 4495 raise SettingWithCopyError(t)4496 if value == "warn":4497 warnings.warn(t, SettingWithCopyWarning, stacklevel=find_stack_level())SettingWithCopyError:
A value is trying to be set on a copy of a slice from a DataFrameSee the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy