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

2024-06-06 00:58:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用

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

然而,我希望能够只使用等于5美元的冰淇淋销售额,以及精确到90度的温度

我将如何使用我感兴趣的特定值,而不是整个数据列


Tags: 数据dfplot温度感兴趣temperaturesalescream
1条回答
网友
1楼 · 发布于 2024-06-06 00:58:22

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

假设您有一个数据框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

另外,请注意不要在变量的上都应用子集,否则您将伪造您试图用散点图显示的任何关系

相关问题 更多 >