使用字符串搜索忽略dataframe中的其他匹配项

2024-09-28 01:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图反复浏览一系列股票名称和价格“AAPL_high”是有效的,因为没有其他股票具有相同的名称

但是,在“F_high”上,下面的代码查找“F_high”,并列出包含字符串的其他股票,例如“CF_high”和“COF_high”。我只需要'F_high',没有其他包含额外字符的字符串

stocks_of_interest = ['AAPL_high', 'F_high']
    df = pd.read_csv('test.csv')
    for i in stocks_of_interest:
            print(i)
            indiv = df[df['level_0'].str.contains(i)]
            print(indiv)

我应该向str.contains()传递另一个参数以进一步细化它,还是使用不同的筛选方法?先谢谢你


Tags: ofcsv字符串df股票printhighcontains
1条回答
网友
1楼 · 发布于 2024-09-28 01:31:17

以匹配dataframe中的字符串。你能行

stocks_of_interest = ['AAPL_high', 'F_high']
    # from listed stocks print out just their correlations
    df = pd.read_csv('test.csv')
    for i in stocks_of_interest:
            print(i)
            indiv = df.iloc[np.where(df['level_0'].values==i)]
            print(indiv)

相关问题 更多 >

    热门问题