简体   繁体   中英

Filtering dataframe based on another dataframe

I have a data frame which has information on 1000 stocks (open, close, high, low, volume, company name, ticker symbol etc.) and I have another dataframe which just has one column of ticker symbols and this second dataframe has fewer rows than 1000 rows in the first dataframe. Now, I want only those rows in the first dataframe for which the ticker symbol is available in the second dataframe. How can this be done using pandas? In my case, I have small dataframes. But I would also like to know how this operation can be scaled up. So, please suggest efficient way as well.

Thanks

You can use .isin() to filter to the list of tickers available in df2.

df1_filtered = df1[df1['ticker'].isin(df2['ticker'].tolist())]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM