Home > Back-end >  python: How can I check if user input is in dataframe
python: How can I check if user input is in dataframe

Time:12-05

I am building a S&P500 app using streamlit, the functionality of the app prompts user to either choose number of plots to be shown from the slider or to type a specific symbol to show its plot, however, I am facing problems in trying to check if the symbol exists in the pandas series which contains all the symbols,(note both the user input and the symbol in the series is of type string) can anybody please tell me how can I fix it

import streamlit as st
import pandas as pd
import base64
import matplotlib.pyplot as plt
import yfinance as yf

@st.cache
def load_data():
    url = "https://en.wikipedia.org/wiki/List_of_S&P_500_companies"
    html = pd.read_html(url, header=0)
    df = html[0]
    return df

df = load_data()
df = df[0]

sector_unique = sorted( df['GICS Sector'].unique() )
selected_sector = st.sidebar.multiselect('Sector', sector_unique, sector_unique)

df_selected_sector = df[(df['GICS Sector'].isin(selected_sector))]
data = yf.download(
        tickers = list(df_selected_sector[:10].Symbol),
        period = "ytd",
        interval = "1d",
        group_by = 'ticker',
        auto_adjust = True,
        prepost = True,
        threads = True,
        proxy = None
    )

num_company = st.sidebar.slider('Number of companies for plots', 1, 10)

spec_symbol = st.sidebar.text_input("OR choose a specific symbol ")

if spec_symbol:
    if(spec_symbol == (a for a in df['Symbol'].items())):
        spec_data = yf.download(
                tickers = spec_symbol,
                period = "ytd",
                interval = "1d",
                group_by = 'ticker',
                auto_adjust = True,
                prepost = True,
                threads = True,
                proxy = None
            )
    else:
        st.sidebar.write("Symbol not found") 

despite the user input symbol being valid or not, it keeps giving me the else statement("symbol not found") here is a sample of the output of df["Symbol"] to make it clearer:

    Symbol
0   MMM
1   AOS
2   ABT
3   ABBV
4   ABMD
5   ACN
6   ATVI
7   ADM
8   ADBE

CodePudding user response:

You should use pandas.Series.tolist (that returns a list) instead of pandas.Series.items (that returns an iterable).

Replace this :

if(spec_symbol == (a for a in df['Symbol'].items())):

By this :

if(spec_symbol == (a for a in df['Symbol'].tolist())):

Or simply :

if spec_symbol in df['Symbol'].tolist():
  • Related