1 回答

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
符號(hào)是您的數(shù)據(jù)幀索引,您需要使用reset_index它來將其放入框架本身。嘗試這個(gè):
df = (pd.DataFrame(web.DataReader(stocks, 'yahoo', day, day)
.iloc[0])
.unstack(level=0)
.droplevel(level=0, axis=1)
.rename_axis(columns=None) # Gets rid of the "Attributes"
.reset_index() # Puts "Symbols" as an actual column, not as the index
)
我的2個(gè)補(bǔ)充:
rename_axis
這應(yīng)該擺脫你的“屬性”標(biāo)題。這主要用于打印時(shí)的視覺目的,但可能會(huì)讓不習(xí)慣使用多索引數(shù)據(jù)的人感到困惑。本質(zhì)上,您的列標(biāo)簽存儲(chǔ)在一個(gè)Index
對象中。該Index
對象可以有一個(gè)名稱,因此“屬性”是列的名稱(非常奇怪的概念,這對于普通索引來說并不是非常有用 - 但在使用 a 時(shí)有很多用處MultiIndex
)。reset_index()
看來您的“符號(hào)”列實(shí)際上并不是一列(這就是為什么它沒有出現(xiàn)在df.columns
數(shù)據(jù)幀的索引中,而是出現(xiàn)在數(shù)據(jù)幀的索引中。添加此方法會(huì)將“符號(hào)”索引作為列插入到數(shù)據(jù)幀中,并且創(chuàng)建一個(gè)簡單的新索引RangeIndex
,范圍從 0 到數(shù)據(jù)幀的長度。
添加回答
舉報(bào)