从枢轴表中删除零值以某种方式造成了另一个问题

发布于 2025-02-02 11:13:17 字数 1352 浏览 3 评论 0原文

我从Python课程中进行了一项分配,以挖掘一组数据,该数据包括(几乎)从1960年到2011年全球所有国家 /地区的二氧化碳排放。在一个特定国家的二氧化碳生产的增长,我想避免将零插入图。这是我一直在使用的代码。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sn
import numpy as np

# Creating DataFrame
Data = pd.read_excel('CO2 Sorted Data.xlsx')
df = pd.DataFrame(Data, columns=['Year','CountryName','Region','IncomeType','CO2Emission','CO2EmCMT'])
df.replace(0,np.nan,inplace=True)
print(df)

# Creating the Pivot Table
pvt = df.pivot_table(index=['Year'],columns=['CountryName'],values='CO2Emission',aggfunc='sum')

# Creating the Graph
pvt2 = pvt.reindex()
CO2Country = input('Input Country Name = ')
remove_zero=pvt2[CO2Country]
rz1=[i for i in remove_zero if i !=0]
plt.plot(rz1,c='red')
plt.title('CO2 Emission of ' +CO2Country +' (1960-2011)', fontsize=10)
plt.xlabel('Year',fontsize=10)
plt.ylabel('CO2 Emission (kiloton)')
plt.grid(True)
plt.show

例如,如果我输入Aruba,则输出看起来像这样。 aruba的线图

但是,x-轴仅显示在该年的'数字'数据要求,而不是一年本身。除了将零更改为NAN之外,我不知道这触发了什么,但这在我看来没有任何意义。与1986 - 2011年一样,我该如何使X轴显示真实的一年?

这是数据的一瞥:

I have an assignment from my Python class to mine a set of data consisting CO2 emissions from (almost) all the countries in the world from 1960 to 2011. One of the task i've been working on is to produce a line graph that represents the growth of CO2 production in a specific country, and i'd like to avoid inserting zeros into the graph. Here is the code i've been using.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sn
import numpy as np

# Creating DataFrame
Data = pd.read_excel('CO2 Sorted Data.xlsx')
df = pd.DataFrame(Data, columns=['Year','CountryName','Region','IncomeType','CO2Emission','CO2EmCMT'])
df.replace(0,np.nan,inplace=True)
print(df)

# Creating the Pivot Table
pvt = df.pivot_table(index=['Year'],columns=['CountryName'],values='CO2Emission',aggfunc='sum')

# Creating the Graph
pvt2 = pvt.reindex()
CO2Country = input('Input Country Name = ')
remove_zero=pvt2[CO2Country]
rz1=[i for i in remove_zero if i !=0]
plt.plot(rz1,c='red')
plt.title('CO2 Emission of ' +CO2Country +' (1960-2011)', fontsize=10)
plt.xlabel('Year',fontsize=10)
plt.ylabel('CO2 Emission (kiloton)')
plt.grid(True)
plt.show

If i input Aruba for example, output would look like this.
Line Graph of Aruba

However, the x-axis only shows the 'number' of years on the data requested, not the year itself. I have no clue on what triggers this other than changing the zeroes to NaN, but that doesn't make any sense in my mind. How can i make the x-axis show the true year, as in 1986-2011?

Here is a glimpse of the data:
A little bit of the data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姜生凉生 2025-02-09 11:13:17

要以适当的年份格式获取输出,您必须先列举数据。

因此:data = list(枚举(rz1,start = 1960))

有一些方法可以绘制此新数据,一个是将数据转换为NP数组和转移,另一个是通过使用zip函数。他们都有相同的输出。

data = list(zip(*b))

data = np.array(data).transpose()

最终代码(在创建图中)为:

# Creating the Graph
pvt2 = pvt.reindex()
CO2Country = 'Aruba'

remove_zero=pvt2[CO2Country]
rz1=[i for i in remove_zero if i !=0]
data = list(enumerate(rz1, start=1960))
# data= np.array(data).transpose()

data= list(zip(*data))
plt.plot(data[0], data[1],c='red')

s/n:呼叫plt.show(),而不是plt.show

To get the output in proper year format, you must enumerate the data first.

So: data = list(enumerate(rz1, start=1960))

There are to ways to go about plotting this new data, one is by converting the data into a np Array and transposing, the other is by using the zip function. They both have the same output.

data = list(zip(*b))

or

data = np.array(data).transpose()

The final code(in the creating the graph section) is:

# Creating the Graph
pvt2 = pvt.reindex()
CO2Country = 'Aruba'

remove_zero=pvt2[CO2Country]
rz1=[i for i in remove_zero if i !=0]
data = list(enumerate(rz1, start=1960))
# data= np.array(data).transpose()

data= list(zip(*data))
plt.plot(data[0], data[1],c='red')

s/n: call plt.show(), not plt.show

北凤男飞 2025-02-09 11:13:17

我不知道枢纽的事情,但是以下效果很好:

co2.Year = pd.to_datetime(co2.Year)
aruba = df[df.CountryName = "Aruba"].set_index("Year")
aruba.CO2Emission.plot()

I don't know about the pivoting thing, but the following works fine:

co2.Year = pd.to_datetime(co2.Year)
aruba = df[df.CountryName = "Aruba"].set_index("Year")
aruba.CO2Emission.plot()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文