python - 从python语言的excel表中获取特定“字符串”的相应单元格值以绘制线/散点图
问题描述
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()
解决方案
在你开始制作情节之前,首先你应该提取关于阿根廷的数据。
import pandas as pd
import matplotlib.pyplot as plt
# Define the headers
headers = ["SeriesName", "CountryName", "Time", "Value"]
# Read in the Excel file
df_raw = pd.read_excel("C:/1/Data3.xlsx",header=None, names=headers)
# extract data to only Argentina
country = ["Argentina"]
# Create a copy of the data with only the Argentina
df = df_raw[df_raw.CountryName.isin(country)].copy()
#print(df)
提取后,您只能使用 Pandas 来制作情节。
'''Pandas plot'''
df.plot.line(x='Time', y='Value', c='Red',legend =0, title = "ARGENTINA GDP per capita")
plt.show()
您还可以通过 Matplotlib 库和 Seaborn 或 Plotly 进行绘图。
# Create plot from matplotlib
plt.figure()
plt.scatter(df.Value, df.Time)
plt.xlabel('GPD Value')
plt.ylabel('Years')
plt.title('''ARGENTINA
GDP per capita (constant 2010 US$) ''')
plt.show()
希伯恩情节
import seaborn as sns
sns.scatterplot(x="Value", y="Time", data=df, color = 'DarkBlue')
plt.subplots_adjust(top=0.9)
plt.suptitle("ARGENTINA GDP per capita")
plt.show()
情节情节
import plotly
import plotly.graph_objs as go
trace = go.Scatter(x = df.Time, y = df.Value)
data = [trace]
plotly.offline.plot({"data": data}, filename='Argentina GDP.html')
推荐阅读
- excel - 如果单元格值包含特定字符串,则添加到列表框
- notepad++ - 使用 Notepad++ 搜索文件中的多个单词
- google-apps-script - 为什么代码格式损坏并且是错误的,不是很有帮助
- python - 比较两个 3d Numpy 数组并返回带有索引的不匹配值,然后在没有循环的情况下重新创建它们
- python-xarray - 无法将 csv 文件中的 1d 变量添加到 Xarray 数据集 - 错误?
- python - 读取文件时如何处理 UnicodeDecodeError?
- quarkus - 如果调用错误,Quarkus REST 服务返回空响应
- javascript - 将自定义js文件添加到cshtml
- javascript - 提高获取请求加载速度
- postgresql-11 - 在 Azure Database for Postgres 的 PaaS 产品中创建超级用户?