1、使用to_numpy()转换
pandas v0.24.0以上版本,可以使用to_numpy()
方法,
例如,
import numpy as np import pandas as pd df = pd.DataFrame(data={'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c']) # 转换整个 DataFrame df.to_numpy() # array([[1, 4, 7], # [2, 5, 8], # [3, 6, 9]]) # 转换指定列 df[['A', 'C']].to_numpy() # array([[1, 7], # [2, 8], # [3, 9]])
2、使用DataFrame中values
import numpy as np import pandas as pd df = pd.DataFrame(data={'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c']) df.values # array([[1, 4, 7], # [2, 5, 8], # [3, 6, 9]])
3、使用DataFrame的as_matrix()
import numpy as np import pandas as pd df = pd.DataFrame(data={'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c']) df.as_matrix() # array([[1, 4, 7], # [2, 5, 8], # [3, 6, 9]])
4、使用Numpy的array()
import numpy as np import pandas as pd df = pd.DataFrame(data={'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c']) np.array(df) # array([[1, 4, 7], # [2, 5, 8], # [3, 6, 9]])