Help Required - EDA Python Code Error

Hello there,

I’m working on an EDA project in Python, and I’d want to see the relationship between three numerical variables: “temperature,” “humidity,” and “pressure.” I intend to construct a heatmap for this purpose, similar to the one shown in this example. I used the following code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Loading the dataset
data = pd.read_csv('weather_data.csv')

# Extracting temperature, humidity, and pressure columns
temperature = data['temperature']
humidity = data['humidity']
pressure = data['pressure']

# Creating the correlation matrix
correlation_matrix = data.corr()

# Creating the heatmap
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()

However, when I run the code, I get the following error:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I’m not sure what caused this mistake or how to solve it. Could someone possibly help me understand the problem with my code and how to appropriately produce the correlation heatmap for the three variables?

Thank you for your assistance!

1 Like

Well, there are some error in data.corr() function.
Here is the correct code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Loading the dataset
data = pd.read_csv('weather_data.csv')

# Extracting temperature, humidity, and pressure columns
temperature = data['temperature']
humidity = data['humidity']
pressure = data['pressure']

# Creating the correlation matrix
correlation_matrix = pd.DataFrame({'temperature': temperature, 'humidity': humidity, 'pressure': pressure}).corr()

# Creating the heatmap
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()

Thanks

1 Like