赞
踩
目录
list(列表)、dict(字典)和tuple(元组)是python的数据类型;
dataframe(表格型的数据结构)、series(定长的有序字典)是pandas的数据类型;
ndarray(数组)是numpy的数据类型;
import numpy as np
import pandas as pd
1、list
list1 = range(10) #[0,1,2,...,9]
list2 = [0] * 5 #[0,0,0,0,0]
initVal = 1
listLen = 5
list3 = [ initVal for i in range(5)] #[1,1,1,1,1]
list4 = [initVal] * listLen #[1,1,1,1,1]
2、dict
d = dict()
d['a'] = 1
3、tuple
当定义一个tuple时,tuple的元素就必须被确定下来。
4、ndarray
a1=np.zeros((3,4))
a2=np.ones((2,3,4), dtype=np.int16)
a3=np.empty((2,3))
a4 = np.arange(10,30,5) # 初始值10,结束值:30(不包含),步长:5
a5 =np.linspace(0, 2, 9)
1、列表
nums=[1, 3, 5, 7, 8, 13, 20];
print tuple(nums) 返回:(1, 3, 5, 7, 8, 13, 20)
注意:列表不可以转为字典
2、字典(dict)
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
print (list(dict)) #返回:['age', 'name', 'class']
print (list(dict.values())) #返回:['Zara', 7, 'First']
3、元组
tup=(1, 2, 3, 4, 5)
print (list(tup)) #返回:[1, 2, 3, 4, 5]
注意:元组不可以转为字典
4、字符串
转列表:list(eval("(1,2,3)"))
转字典:dict(eval("{'name':'ljq', 'age':24}"))
转元组:tuple(eval("(1,2,3)"))
5、dataframe、series、ndarray
# 以list为基础
data = [[2000, 'Ohino', 1.5],
[2001, 'Ohino', 1.7],
[2002, 'Ohino', 3.6],
[2001, 'Nevada', 2.4],
[2002, 'Nevada', 2.9]] # type(data) 为 list
# list to series
ser = pd.Series(data, index = ['one', 'two', 'three', 'four', 'five'])
返回:
one [2000, Ohino, 1.5]
two [2001, Ohino, 1.7]
three [2002, Ohino, 3.6]
four [2001, Nevada, 2.4]
five [2002, Nevada, 2.9]
# list to dataframe
df = pd.DataFrame(data, index = ['one', 'two', 'three', 'four', 'five'], columns = ['year', 'state', 'pop'])
返回:
year state pop
one 2000 Ohino 1.5
two 2001 Ohino 1.7
three 2002 Ohino 3.6
four 2001 Nevada 2.4
five 2002 Nevada 2.9
# list to array
ndarray = np.array(data) (有一个是字符,所以都转为了字符)
返回:
[['2000' 'Ohino' '1.5']
['2001' 'Ohino' '1.7']
['2002' 'Ohino' '3.6']
['2001' 'Nevada' '2.4']
['2002' 'Nevada' '2.9']]
# 以dict为基础
dic={'one':[1,2,3],'two':[2,3,4],'three':[3,4,5]}
df2=pd.DataFrame(dic)
返回:
one two three
0 1 2 3
1 2 3 4
2 3 4 5
ser2 = pd.Series(dic)
返回:
one [1, 2, 3]
two [2, 3, 4]
three [3, 4, 5]
ndarray2 = np.array(dic)
返回:
{'one': [1, 2, 3], 'two': [2, 3, 4], 'three': [3, 4, 5]}
########### list ###########
dict <--> list
*list* = *dict*.values() # list of values
*list* = *dict*.keys() # list of keys
*list* = list(*dict*)
*dict* = dict(*list*)
ndarray <--> list
*list* = *ndarray*.tolist()
*ndarray* = np.array(*list*)
tuple <--> list
*list* = list(*tuple*)
*tuple* = tuple(*list*)
########### Series ###########
Series <--> DataFrame
*dataframe* = pd.DataFrame({"XXX1":*series1*,"XXX2":*series2*})
*series* = *dataframe*[0] #无标签时
*series* = *dataframe*["XXX"] #有标签时
Serise <--> ndarray
*series* = pd.Series(*ndarray*) #这里的ndarray是1维的
*ndarray* = np.array(*series*)
*ndarray* = *series*.values
Series <--> list
*series* = pd.Series(*list*)
*list* = *series*.tolist()
*list* = list(*series*)
########### DataFrame ###########
DataFrame <--> ndarray
*ndarray* = *dataframe*.values
*dataframe* = pd.DataFrame(*ndarray*)
DataFrame <--> list
*list* = *dataframe*.values.tolist()
*dataframe* = pd.DataFrame(*list*)
DataFrame <--> dict
*dataframe* = pd.DataFrame.from_dict({0:*dict1*, 1:*dict2*})
*dict* = *dataframe*.to_dict()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。