python的dataframe转换为多维矩阵有两种方法,一种利用as_matrix()属性,一种利用values
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(3,4),columns=list('abcd'),index=list('ABC'))
print(df)
print('======values======')
print(df.values)
print('======as_matrix======')
print(df.as_matrix())
运行结果如图:
a b c d
A 0.854229 0.264031 0.289637 0.336938
B 0.308044 0.268466 0.145303 0.686527
C 0.805049 0.370888 0.406713 0.242808
======values======
[[ 0.85422936 0.26403105 0.28963703 0.33693802]
[ 0.30804358 0.26846622 0.14530251 0.68652656]
[ 0.80504923 0.370888 0.40671339 0.24280768]]
======as_matrix======
[[ 0.85422936 0.26403105 0.28963703 0.33693802]
[ 0.30804358 0.26846622 0.14530251 0.68652656]
[ 0.80504923 0.370888 0.40671339 0.24280768]]