Home > other >  Join tables and only keep right ones, but keep amount of left ones
Join tables and only keep right ones, but keep amount of left ones

Time:09-30

I have two tables:

df1:

index   Address                                     type    amount   Epoch  ln of amount
0       0x88aDa02f6fCE2F1A833ad9B4999D62a7e3b70367  outflow 250000.0    1   12.429216
1       0x88aDa02f6fCE2F1A833ad9B4999D62a7e3b70367  outflow 250000.0    2   12.429216
2       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389  outflow 250000.0    1   12.429216
3       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389  outflow 33333.0     2   10.414303
4       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389  outflow 33333.0     3   10.414303

and df2:

Index   Other Addresses                
0       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389

I want to create a new table that only contains the addresses (and all other respective columns) from df2, but the amount of rows needs to be the one on the left.

Basically, I am identifying which addresses from df2 exist in df1, and keep only those, with all of their information from other columns.

In my example, the resulting table would look like this:

index   Address                                     type    amount   Epoch  ln of amount
0       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389  outflow 250000.0    1   12.429216
1       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389  outflow 33333.0     2   10.414303
2       0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389  outflow 33333.0     3   10.414303

CodePudding user response:

You can use pandas.Series.isin, as suggested in the comments:

df1[df1['Address'].isin(df2['Other Addresses])

Example:

df1=pd.DataFrame.from_dict({'col1':[1,2,3],'col2':['x','y','z']})

ie df1 is:

   col1 col2
0     1    x
1     2    y
2     3    z

Create df2 too:

df2=pd.DataFrame.from_dict({'col1_other':[1,7]})

ie:

   col1
0     1
1     7

df1[df1['col1'].isin(df2['col1_other'])] then becomes:

   col1 col2
0     1    x
  • Related