Select dataframe index derived from comparing a dataframe column and a list

Costa.Gustavo

Considering the instrument_ticker dataframe and tickers list below:

import pandas as pd
import numpy as np

stk_off = pd.DataFrame({'Ticker': ['EWZ US 05/29/20 P27', 'HSI US 12/30/20 C24800', 'TLT US 06/19/20 C225', 'EWZ US 05/29/20 P29'],
                   'Instrument': ['EWZ', 'HSI', 'TLT', 'EWZ'],
                   'Maturity Label': ['MAI 20 D29', 'DEC 20 D30', 'JUN 20 D19', 'MAY 20 D29'],
                   'Strike': [27, 24800, 225, 29],
                   'Payout': ['P', 'C', 'C', 'P'],
                   'Maturity': ['29/05/2020', '12/30/2020', '19/06/2020', '29/05/2020'],
                   'Market': ['US NYSE', 'US NYSE', 'HK HKSE', 'US NYSE']})

stk_off['Reduced_Ticker'] = stk_off['Ticker'].apply(lambda a :" ".join(a.split(" ", 2)[:2]))

instrument_ticker = stk_off[['Instrument','Reduced_Ticker']].groupby(['Instrument']).agg(list)
instrument_ticker['Reduced_Ticker'] = instrument_ticker['Reduced_Ticker'].apply(lambda x: pd.unique(x))

ticker = ['EWZ US 05/29/20 C31','HSI US 12/30/20 C24900'] 

I do a split to select the first item of each value obtained from this operation of each line:

tickers_reduced = [t.split()[0] for t in tickers]

Now, how can I get the index of instrument_ticker where Reduced_Ticker contains a ticker_reduced item? I need to save this in a list, like the following (expected output):

instrument = ['EWZ', 'HSI']
Toukenize

Select what you require by checking the first split term of Reduced_Ticker, followed by obtaining the index of the selected rows:

selection = (
    instrument_ticker
    .explode('Reduced_Ticker')
    ['Reduced_Ticker']
    .apply(lambda x : x.split()[0] in tickers_reduced)
)

instrument = instrument_ticker.loc[selection].index.to_list()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select values from different Dataframe column based on a list of index

how to conditionally select dataframe rows by comparing column values with a list

Comparing Pandas Dataframe column with List

Get country name from dataframe column by comparing with a list

Select column from DataFrame rows using a list of column labels

Select specific index, column pairs from pandas dataframe

Pandas dataframe comparing with no index

Select DataFrame column elements that are in a list

Remove column index from dataframe

Python class derived from pandas DataFrame with list/DataFrame attribute

How to select all elements from column that it is a list in DataFrame in python?

Select rows from a DataFrame based on list values in a column in pandas

How to select specific column items as list from pandas dataframe?

pandas dataframe select list value from another column

Select a partial string from a list within a column in Pandas DataFrame

Comparing a list to a dataframe column and create new column with numbers

dataframe, set index from list

Add column in dataframe from list

Replace values from one column by comparing another column to a second DataFrame

Adding column to DataFrame based on index in list

Finding the index of an element is list in a Pandas Dataframe column

Adding index column to each dataframe in a list R

Finding List Index of Values in Dataframe Column

Casting a new derived column in a DataFrame from boolean to integer

Create dataframe with new columns derived from unique values in a single column

How to filter pandas dataframe to select list of column

Returning rows in dataframe by comparing on a column

comparing a dataframe column to the values found in another dataframe

How to select column from from DATAFRAME with condition

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive