Comparing matching operators in R and pandas

Here, we will demonstrate the equivalence of matching operators between R (%in%) and pandas (isin()). In both cases, a logical vector or series (pandas) is produced, which indicates the position at which a match was found.

R %in% operator

Here, we will demonstrate the use of the %in% operator in R:

>stock_symbols=stocks_table$Symbol
>stock_symbols
[1] GOOG  AMZN  FB  AAPL  TWTR  NFLX  LINKD
Levels: AAPL AMZN FB GOOG LINKD NFLX TWTR

>stock_symbols %in% c('GOOG','NFLX')
[1]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE

The pandas isin() function

Here is an example of using the pandas isin() function:

In [11]: stock_symbols=stocks_df.Symbol
stock_symbols
Out[11]: 0    GOOG
         1    AMZN
         2      FB
         3    AAPL
         4    TWTR
         5    NFLX
         6    LNKD
         Name: Symbol, dtype: object
In [10]: stock_symbols.isin(['GOOG','NFLX'])
Out[10]: 0     True
         1    False
         2    False
         3    False
         4    False
         5     True
         6    False
         Name: Symbol, dtype: bool
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232