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()
作者:jinqier
出处:http://www.cnblogs.com/jinqier/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。