加入表格并只保留右边的表格,但保留左边的表格数量
Join tables and only keep right ones, but keep amount of left ones
我有两个 table:
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
和 df2:
Index Other Addresses
0 0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389
我想创建一个新的 table,它只包含来自 df2 的地址(以及所有其他相应的列),但是行数需要在左边。
基本上,我正在识别 df2 中的哪些地址存在于 df1 中,并且只保留那些地址,以及来自其他列的所有信息。
在我的示例中,生成的 table 将如下所示:
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
您可以使用 pandas.Series.isin
,如评论中所建议:
df1[df1['Address'].isin(df2['Other Addresses])
示例:
df1=pd.DataFrame.from_dict({'col1':[1,2,3],'col2':['x','y','z']})
即df1
是:
col1 col2
0 1 x
1 2 y
2 3 z
也创建df2
:
df2=pd.DataFrame.from_dict({'col1_other':[1,7]})
即:
col1
0 1
1 7
df1[df1['col1'].isin(df2['col1_other'])]
则变为:
col1 col2
0 1 x
我有两个 table:
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
和 df2:
Index Other Addresses
0 0xhj2gf34jhl23h23ljhn45b1836hfñlsxsdkjv389
我想创建一个新的 table,它只包含来自 df2 的地址(以及所有其他相应的列),但是行数需要在左边。
基本上,我正在识别 df2 中的哪些地址存在于 df1 中,并且只保留那些地址,以及来自其他列的所有信息。
在我的示例中,生成的 table 将如下所示:
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
您可以使用 pandas.Series.isin
,如评论中所建议:
df1[df1['Address'].isin(df2['Other Addresses])
示例:
df1=pd.DataFrame.from_dict({'col1':[1,2,3],'col2':['x','y','z']})
即df1
是:
col1 col2
0 1 x
1 2 y
2 3 z
也创建df2
:
df2=pd.DataFrame.from_dict({'col1_other':[1,7]})
即:
col1
0 1
1 7
df1[df1['col1'].isin(df2['col1_other'])]
则变为:
col1 col2
0 1 x