如何使用 Pandas 使用列中的特定数据而不是列中的所有数据创建散点图

How to create a scatter plot using Pandas, with specific data from a column, and not all of the data in a column

我目前正在使用

df.plot.scatter(x='Ice_cream_sales', y='Temperature')

但是,我希望只能使用等于 5 美元的冰淇淋销售额,以及恰好在 90 度的温度。

我将如何着手使用我感兴趣的特定值,代替整列数据?

最简单的方法是创建您感兴趣的值子集的数据框。

假设您有一个包含列 'Ice_cream_sales'、'Temperature'

的数据框 df
import pandas as pd
import matplotlib.pyplot as plt

# Here we subset your dataframe where the temperature is 90, which will give you a 
# boolean array for your dataframe.
temp_90 = df['Temperature'] == 90

# Apply your boolean against your dataframe to grab the correct rows:
df2 = df[temp_90]

# Now plot your scatter plot
plt.scatter(x=df2['ice_cream_sales'] y=df2['Temperature'])
plt.show()

我不确定您为什么要绘制一个散点图,其中销售额 = 5 美元,温度 = 90。这会给您一个数据点。

相反,您可以使用不等式进行子集化:

high_temp = df['Temperature'] >= 90

另外请注意不要对变量的 都应用子集,否则你会伪造你试图用散点图显示的任何关系。