【发布时间】:2019-06-13 20:39:05
【问题描述】:
我想在整个数据中为country name == 'Argentina' 与其对应的“值”绘制一个线/散点图。
样本数据
这是我的代码
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel("C:/Users/kdandebo/Desktop/Models/Python excercise/Data3.xlsx")
x = (df['Country Name'])
#Although i have figured out x cannot be compared to a string named Argentina, i couldnt think of any other way, Also ive tried the below version too, but none works
#if (df['Country Name'] == 'Argentina'):
# y = (df['Value'])
for x == ("Argentina"):
y = (df['Value'])
plt.scatter(x,y)
plt.show()
【问题讨论】:
-
编程的第一步很难,每个人都有不同的学习方式。然而,Python 有一个很大的优势,因为它很容易看到一个人试图做什么,因此自己可以看到哪里出了问题。你只需要 2 行来制作你想要的东西,1 行来读取 excel,1 行来制作情节,你可以通过 df.plot() 来完成。究竟你需要写什么,我会让你从与绘图相关的 pandas 文档中找出答案。如果你在 spyder 中,你可以使用调试器来查看你的代码实际做了什么,这是一种很好的学习方式。
-
xlsx 文件或需要的样本
-
您可以检查的一件事是当您编写
for x == ('Argentina')时会发生什么。我很确定这不会达到您的预期目标。 -
将整个文件添加为“总数据文件”
-
是的,我正在使用 Spyder,是的,我尝试过调试,但不清楚
标签: python python-3.x python-2.7 python-requests spyder