array 转 DataFrame:
df = pd.DataFrame(array)
DataFrame 转 array:
array = df.values
示例:
-
读入数据
import pandas as pd import numpy as npusers = pd.read_table('../../dataset/ml-1m/users.dat', sep='::', header=None, engine='python', encoding='utf-8').to_numpy()
运行结果:
-
array 转 DataFrame,并添加列标签
unames = ['userId', 'gender', 'age', 'occupation', 'zipCode'] users = pd.DataFrame(users, columns=unames)
运行结果
-
DataFrame 转 array
users.values
运行结果: