Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -57,9 +57,16 @@ def color_combined_score(value):
|
|
57 |
color = 'none'
|
58 |
return f'background-color: {color};'
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# User interface in Streamlit
|
61 |
st.title('S&P 500 Stock Comparison Tool')
|
62 |
|
|
|
63 |
# Load the current S&P 500 list and averages
|
64 |
sp500_list = get_sp500_list()
|
65 |
sp500_averages = load_sp500_averages(sp500_averages_path)
|
@@ -68,21 +75,22 @@ sp500_averages = load_sp500_averages(sp500_averages_path)
|
|
68 |
scores_df = calculate_combined_scores_for_stocks(sp500_list, sp500_averages)
|
69 |
scores_df_sorted = scores_df.sort_values(by='Combined Score', ascending=False)
|
70 |
|
|
|
|
|
|
|
71 |
# Layout for displaying overview and details
|
72 |
col1, col2 = st.columns([3, 5]) # For example, this will give the first column 3/8 of the width
|
73 |
|
74 |
with col1:
|
75 |
st.subheader("Stock Overview")
|
76 |
-
# Make sure to convert 'Combined Score' to numeric if it's not already
|
77 |
-
scores_df_sorted['Combined Score'] = pd.to_numeric(scores_df_sorted['Combined Score'], errors='coerce')
|
78 |
# Apply color based on 'Combined Score' value and display the DataFrame
|
79 |
-
styled_scores_df =
|
80 |
-
st.dataframe(styled_scores_df
|
81 |
|
82 |
with col2:
|
83 |
st.subheader("Stock Details")
|
84 |
-
#
|
85 |
-
sorted_tickers =
|
86 |
ticker_symbol = st.selectbox('Select a stock for details', options=sorted_tickers)
|
87 |
if ticker_symbol:
|
88 |
with st.spinner(f'Fetching data for {ticker_symbol}...'):
|
|
|
57 |
color = 'none'
|
58 |
return f'background-color: {color};'
|
59 |
|
60 |
+
def filter_incomplete_stocks(df):
|
61 |
+
# Assuming that 'N/A' represents missing data, replace it with NaN
|
62 |
+
df.replace('N/A', pd.NA, inplace=True)
|
63 |
+
# Drop rows with any NaN values in the specified columns
|
64 |
+
return df.dropna(subset=['P/E Ratio', 'P/B Ratio', 'P/S Ratio', 'Debt to Equity Ratio', 'Return on Equity', 'Book-to-Market Ratio'])
|
65 |
+
|
66 |
# User interface in Streamlit
|
67 |
st.title('S&P 500 Stock Comparison Tool')
|
68 |
|
69 |
+
|
70 |
# Load the current S&P 500 list and averages
|
71 |
sp500_list = get_sp500_list()
|
72 |
sp500_averages = load_sp500_averages(sp500_averages_path)
|
|
|
75 |
scores_df = calculate_combined_scores_for_stocks(sp500_list, sp500_averages)
|
76 |
scores_df_sorted = scores_df.sort_values(by='Combined Score', ascending=False)
|
77 |
|
78 |
+
# Filter out stocks with incomplete data
|
79 |
+
scores_df_filtered = filter_incomplete_stocks(scores_df_sorted)
|
80 |
+
|
81 |
# Layout for displaying overview and details
|
82 |
col1, col2 = st.columns([3, 5]) # For example, this will give the first column 3/8 of the width
|
83 |
|
84 |
with col1:
|
85 |
st.subheader("Stock Overview")
|
|
|
|
|
86 |
# Apply color based on 'Combined Score' value and display the DataFrame
|
87 |
+
styled_scores_df = scores_df_filtered.style.applymap(color_combined_score, subset=['Combined Score'])
|
88 |
+
st.dataframe(styled_scores_df)
|
89 |
|
90 |
with col2:
|
91 |
st.subheader("Stock Details")
|
92 |
+
# Use the filtered DataFrame to create the dropdown for stock selection
|
93 |
+
sorted_tickers = scores_df_filtered['Stock'].tolist()
|
94 |
ticker_symbol = st.selectbox('Select a stock for details', options=sorted_tickers)
|
95 |
if ticker_symbol:
|
96 |
with st.spinner(f'Fetching data for {ticker_symbol}...'):
|