dibend commited on
Commit
7ed2a2c
1 Parent(s): 68f4690

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -10,19 +10,36 @@ def plot_real_estate_correlation(state):
10
  # Filter for the given state
11
  df = df[df['State'] == state.upper()]
12
 
13
- # Extract the list of ZIP codes and price data
14
  zip_codes = df['RegionName'].unique()
15
- price_data = df.iloc[:, 7:] # Assuming price data starts from the 8th column
16
 
17
- # Drop rows with missing data to avoid issues in correlation calculation
18
- price_data = price_data.dropna(axis=1, how='all')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Calculate the correlation matrix for ZIP codes
21
- corr_matrix = price_data.T.corr()
22
 
23
  # Prepare the grid data for 3D plot
24
- x_data, y_data = np.meshgrid(zip_codes, zip_codes)
25
  z_data = corr_matrix.values
 
26
 
27
  # Create the 3D surface plot
28
  fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
 
10
  # Filter for the given state
11
  df = df[df['State'] == state.upper()]
12
 
13
+ # Extract the list of ZIP codes and dates
14
  zip_codes = df['RegionName'].unique()
15
+ dates = pd.to_datetime(df.columns[7:]) # Assuming price data starts from the 8th column
16
 
17
+ # Initialize a DataFrame to hold price data for correlation calculation
18
+ price_matrix = []
19
+
20
+ # Loop through each ZIP code in the state
21
+ for zip_code in zip_codes:
22
+ df_zip = df[df['RegionName'] == zip_code]
23
+
24
+ # Extract only the columns with date data (price values)
25
+ prices = df_zip.iloc[0, 7:].values # Extract price values starting from the 8th column
26
+
27
+ # Append prices to the matrix if there are no missing values
28
+ if not np.isnan(prices).all():
29
+ price_matrix.append(prices)
30
+
31
+ # Convert to DataFrame for easier manipulation
32
+ price_matrix_df = pd.DataFrame(price_matrix, index=zip_codes)
33
+
34
+ # Transpose to align for correlation calculation (each column = ZIP code)
35
+ price_matrix_df = price_matrix_df.T.dropna()
36
 
37
  # Calculate the correlation matrix for ZIP codes
38
+ corr_matrix = price_matrix_df.corr()
39
 
40
  # Prepare the grid data for 3D plot
 
41
  z_data = corr_matrix.values
42
+ x_data, y_data = np.meshgrid(zip_codes, zip_codes)
43
 
44
  # Create the 3D surface plot
45
  fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])