python下DataFrame, Series, ndarray, list, dict, tuple的初始化與轉換(整理)

目錄

1.定義

2.引入包

3.空創建

4.初始化

5.相互轉化


1.定義

list(列表)、dict(字典)和tuple(元組)是python的數據類型;

dataframe(表格型的數據結構)、series(定長的有序字典)是pandas的數據類型;

ndarray(數組)是numpy的數據類型;

2.引入包

import numpy as np
import pandas as pd

3.空創建

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) 

4.初始化

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]}

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()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章