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*)
Series <--> dict
*series* = pd.Series(*dict*)
*dict* = *series*.to_dict()
########### 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*})
*dataframe* = pd.DataFrame(*dict*)
*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()
python 数据处理 list serise dataframe ndarray dict tuple之间的相互转化
最新推荐文章于 2024-04-27 21:08:15 发布