Python数据分析(四)DataFrame, Series, ndarray, list, dict, tuple的相互转换

转自:https://blog.csdn.net/lambsnow/article/details/78517340

 

import numpy as np
import pandas as pd

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

########### 其它 list ###########
dict --> list
*list* = *dict*.values() # list of values
*list* = *dict*.keys() # list of keys
*list* = list(*dict*) # list of keys

ndarray <--> list
*list* = *ndarray*.tolist()
*ndarray* = np.array(*list*)

tuple <--> list
*list* = list(*tuple*)
*tuple* = tuple(*list*)

Index --> list
*dataframe*.columns.tolist()

 

posted on 2018-08-05 09:19  jin_qi_er  阅读(4625)  评论(0编辑  收藏  举报