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

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

創建數據透視表的圖表

創建數據透視表的圖表

POPMUISE 2023-10-26 15:18:43
我創建一個數據透視表,我想創建一個條形圖。這是我的數據透視表:我不知道如何提取 1970 列的值并使用此信息制作條形圖。
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

只需將數據框列名轉換為str即可選擇年份的1970數據df['1970']。然后,您可以使用pandas內置plot.bar方法來制作條形圖。嘗試這個:


import pandas as pd

import matplotlib.pyplot as plt


#converting column names to string

df.columns = df.columns.astype(str)


#plotting a bar plot

df['1970'].plot.bar()

plt.show()

基于@AlanDyke DataFrame 的示例:


import pandas as pd

import matplotlib.pyplot as plt


df = pd.DataFrame([[1970,'a',1],

                   [1970,'b',2],

                   [1971,'a',2],

                   [1971,'b',3]],

                   columns=['year','location', 'value'])

df = pd.pivot_table(df, values='value', index='location', columns='year')

df.columns = df.columns.astype(str)

df['1970'].plot.bar()


plt.show()

https://img1.sycdn.imooc.com/653a130f00011df405990447.jpg

查看完整回答
反對 回復 2023-10-26
?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

您可以使用 plt.bar 并對數據幀進行切片:


df = pd.DataFrame([[1970,'a',1],

                   [1970,'b',2], 

                   [1971,'a',2],

                   [1971,'b',3]],

                   columns=['year','location', 'value'])

df = pd.pivot_table(df, values='value', index='location', columns='year')

plt.bar(list(df.transpose().columns), height=df[1970])


查看完整回答
反對 回復 2023-10-26
  • 2 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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