更新时间:2018-08-10 来源:黑马程序员技术社区 浏览量:
01
对list转换的操作#coding:utf-8
import numpy as np
import pandas as pd
#创建list
data=[[2016,'hello',90],
[2017,'world',70],
[2018,'nice',80]]
#把list转为series
ser = pd.Series(data,
index=['one','two','three'])
#把seires转为matrix
mat = ser.as_matirx()
#list转为Dataframe
df=np.Dataframe(data,
index=['one','two','three'],
columns=['year','flag','status'])
#dataframe转为ndarray
array1 = df.as_matrix()
array2 = df.values
array3 = np.array(df)
array_1 = df.as_matrix(['status'])
02
对dict转换的操作#coding:utf-8
import numpy as np
import pandas as pd
#创建dict
data = {'year':[2016,2017,2018],
'flag':['hello','world','nice'],
'status':[90,70,80]}
#把dict转为seires;
#不指定index时,data的key则会为Series的index
ser = pd.Series(data)
#把dict转为Dataframe;
#不指定columns,data的key充当DataFrame的columns
df = pd.DataFrame(data)
03
对array转换的操作#coding:utf-8
import numpy as np
import pandas as pd
#创建数组array
data=np.array([[2016,'hello',90],
[2017,'world',70],
[2018,'nice',80]])
#数组转为DataFrame
df = pd.DataFrame(data,
index=['one','two','three'],
columns=['year','flag','status'])
#DataFrame转为矩阵(数组)
array1 = df.as_matrix()
array2 = df.values
array3 = np.array(df)
#将某一列转为数组
array_1 = df.as_matrix(['status'])
作者:黑马程序员javaEE培训学院
首发:http://java.itheima.com/