Home > Blockchain >  Match 1st dataframe's column 'items' with 2nd dataframe's columns, if match then
Match 1st dataframe's column 'items' with 2nd dataframe's columns, if match then

Time:04-26

I have tried this below code, values matched but result stored under same column i.e, "items" :

'''

for ticker in tickers:
 asset = str(int(df['asset'].values[0]))
 ...
 match = [ticker, asset, debt, debtc]
 def checkIfValuesExists1(df, ticker):
  for ele in ticker:
   if ele in df['items'].values:
    out[ele] = match      
   return out
 out = checkIfValuesExists1(df, match)

'''

ticker items
AAPL 4564
MSFT 7778
GOOGL 7654
ticker asset debt debtc
AAPL 4564 2424 4567
MSFT 7708 7778 8579
GOOGL 7954 2342 7654
ticker asset debt debtc
AAPL 4564
MSFT 7778
GOOGL 7654

CodePudding user response:

You can compare the items column to the all other three columns in df2 based on ticker column

m = df2.set_index('ticker').eq(df1.set_index('ticker')['items'], axis=0)
print(m)

        asset   debt  debtc
ticker
AAPL     True  False  False
MSFT    False   True  False
GOOGL   False  False   True

Then use where to turn the False part to NaN

df_ = df2.set_index('ticker').where(m, np.nan).astype('Int64').reset_index()
print(df_)

  ticker  asset  debt  debtc
0   AAPL   4564  <NA>   <NA>
1   MSFT   <NA>  7778   <NA>
2  GOOGL   <NA>  <NA>   7654

CodePudding user response:

You could also do:

(df2.melt(value_name = 'items').merge(df1).
      pivot('ticker', 'variable').reset_index())
 
         ticker items            
variable        asset  debt debtc
0          AAPL  4564   NaN   NaN
1         GOOGL   NaN   NaN  7654
2          MSFT   NaN  7778   NaN
  • Related