python中如何dataframe轉(zhuǎn)換為ndarray?
小編介紹過python中ndarray與series如何相互轉(zhuǎn)換的方法,其實Series轉(zhuǎn)換為ndarray是一個一維數(shù)組,作為pandas另一個數(shù)據(jù)結(jié)構(gòu),DataFrame轉(zhuǎn)換為ndarray是一個多維數(shù)組,那是怎么轉(zhuǎn)換的呢?本文介紹python中dataframe轉(zhuǎn)換為ndarray的兩種方法。
1、利用dataframevalues屬性將使用所有的列轉(zhuǎn)換為ndarray對象。
frompandasimportSeries,DataFrame
fromnumpyimportarray
if__name__=='__main__':
data=[
[11.53,11.69,11.70,11.51,871365.0,000001],
[11.64,11.63,11.72,11.57,722764.0,000001],
[11.59,11.48,11.59,11.41,461808.0,000001],
[11.39,11.19,11.40,11.15,1074465.0,000001]]
df=DataFrame(data,index=["2017-10-18","2017-10-19","2017-10-20","2017-10-23"],
columns=["open","close","high","low","volume","code"])
printdf
printdf.as_matrix(['open','close'])
printdf.values
printarray(df)
2、通過values方法,實現(xiàn)dataframe轉(zhuǎn)換為ndarray。
importpandasaspd
data=[['2019/08/01',10],
['2019/08/01',11]]
result=pd.DataFrame(data,columns=['ds','val'])
result.values
以上就是python中dataframe轉(zhuǎn)換為ndarray的方法,希望能對你有所幫助喲~更多Python學(xué)習(xí)教程請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。