亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

蟒蛇堆積面積圖

蟒蛇堆積面積圖

忽然笑 2022-09-13 19:39:19
我正在嘗試創建一個堆積面積圖,顯示課程及其數量隨時間的變化。所以我的數據框是(索引=年):                    Area  CoursesYear                             1900         Agriculture      0.01900        Architecture     32.01900           Astronomy     10.01900             Biology     20.01900           Chemistry     25.01900   Civil Engineering     21.01900           Education     14.01900  Engineering Design     10.01900             English     30.01900           Geography      1.0去年:2011年。我嘗試了幾種解決方案,例如df.plot.area(),df.plot.area(x='年')。然后我認為將區域作為列會有所幫助,所以我嘗試了df.pivot_table(index = 'Year', columns = 'Area', values = 'Courses', aggfunc = 'sum')但是我沒有獲得每年的課程總和,而是得到了:Area  Aeronautical Engineering  ...  Visual DesignYear                            ...               1900                       NaN  ...            NaN1901                       NaN  ...            NaN感謝您的幫助。這是我的第一篇文章。抱歉,如果我錯過了什么。更新。這是我的代碼:df = pd.read_csv(filepath, encoding= 'unicode_escape')df = df.groupby(['Year','GenArea'])['Taught'].sum().to_frame(name = 'Courses').reset_index()plt.stackplot(df['Year'], df['Courses'], labels = df['GenArea'])plt.legend(loc='upper left')plt.show()以下是數據集的鏈接:https://data.world/makeovermonday/2020w12
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

有了額外的給定信息,我做了這個。希望你喜歡它!


import pandas as pd

import matplotlib.pyplot as plt


plt.close('all')


df=pd.read_csv('https://query.data.world/s/djx5mi7dociacx7smdk45pfmwp3vjo',

               encoding='unicode_escape')

df=df.groupby(['Year','GenArea'])['Taught'].sum().to_frame(name=

             'Courses').reset_index()

aux1=df.duplicated(subset='GenArea', keep='first').values

aux2=df.duplicated(subset='Year', keep='first').values


n=len(aux1);year=[];courses=[]


for i in range(n):

    if not aux1[i]:

        courses.append(df.iloc[i]['GenArea'])

    if not aux2[i]:

        year.append(df.iloc[i]['Year'])

    else:

        continue


del aux1,aux2

df1=pd.DataFrame(index=year)

s=0


for i in range(len(courses)):

    df1[courses[i]]=0

for i in range(n):

    string=df.iloc[i]['GenArea']

    if any(df1.iloc[s].values==0):

        df1.at[year[s],string]=df.iloc[i]['Courses']

    else:

        s+=1

        df1.at[year[s],string]=df.iloc[i]['Courses']


del year,courses,df

df1=df1[df1.columns[::-1]]

df1.plot.area(legend='reverse')


查看完整回答
反對 回復 2022-09-13
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號