更新時間:2018-08-10 來源:黑馬程序員技術(shù)社區(qū) 瀏覽量:
01
對list轉(zhuǎn)換的操作#coding:utf-8
import numpy as np
import pandas as pd
#創(chuàng)建list
data=[[2016,'hello',90],
[2017,'world',70],
[2018,'nice',80]]
#把list轉(zhuǎn)為series
ser = pd.Series(data,
index=['one','two','three'])
#把seires轉(zhuǎn)為matrix
mat = ser.as_matirx()
#list轉(zhuǎn)為Dataframe
df=np.Dataframe(data,
index=['one','two','three'],
columns=['year','flag','status'])
#dataframe轉(zhuǎn)為ndarray
array1 = df.as_matrix()
array2 = df.values
array3 = np.array(df)
array_1 = df.as_matrix(['status'])
02
對dict轉(zhuǎn)換的操作#coding:utf-8
import numpy as np
import pandas as pd
#創(chuàng)建dict
data = {'year':[2016,2017,2018],
'flag':['hello','world','nice'],
'status':[90,70,80]}
#把dict轉(zhuǎn)為seires;
#不指定index時,data的key則會為Series的index
ser = pd.Series(data)
#把dict轉(zhuǎn)為Dataframe;
#不指定columns,data的key充當(dāng)DataFrame的columns
df = pd.DataFrame(data)
03
對array轉(zhuǎn)換的操作#coding:utf-8
import numpy as np
import pandas as pd
#創(chuàng)建數(shù)組array
data=np.array([[2016,'hello',90],
[2017,'world',70],
[2018,'nice',80]])
#數(shù)組轉(zhuǎn)為DataFrame
df = pd.DataFrame(data,
index=['one','two','three'],
columns=['year','flag','status'])
#DataFrame轉(zhuǎn)為矩陣(數(shù)組)
array1 = df.as_matrix()
array2 = df.values
array3 = np.array(df)
#將某一列轉(zhuǎn)為數(shù)組
array_1 = df.as_matrix(['status'])
作者:黑馬程序員javaEE培訓(xùn)學(xué)院
首發(fā):http://java.itheima.com/