使用 python 语言从 excel sheet 获取特定 'string' 的相应单元格值,用于绘制 line/scatter 图

Getting the corresponding cell value for a specific 'string' from excel sheet in python language for plotting a line/scatter plot

我想在整个数据中绘制 line/scatter 图 country name == 'Argentina' 与其对应的 'value'。

示例数据

total data file

这是我的代码

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

df = pd.DataFrame({'Series Name': ['GDP']*4,
                   'Country Name': ['Argentina']*2 + ['Bolivia']*2,
                   'Time': [2001, 2002, 2001, 2002],
                   'Value': [1, 3, 2, 4]})
#print(df)
df[df['Country Name'] == 'Argentina'].plot.scatter('Time', 'Value')

此类问题的答案通常可以在示例或教程下的库文档中找到。

主要问题是读取传播 sheet 文件并选择正确的 sheet

import pandas as pd
import matplotlib.pyplot as plt

xl = pd.ExcelFile("Data3.xlsx")
df=xl.parse("Data")

x = df[df['Country Name']=="Argentina"]
plt.scatter(x['Country Name'],x['Value'])
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()

enter image description here

Seaborn 情节

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')