dataframe, series, list, dictionary, array之间互相转换

Example
dataframe, series, list, dictionary, array之间互相转换

1. Dataframe

dataframe → series

# dataframe
series_col1 = pd.series
print(series_col1)
print(type(series_col1))

dataframe, series, list, dictionary, array之间互相转换

dataframe → array

array = df.values
print(array)
print(type(array))

dataframe, series, list, dictionary, array之间互相转换

dataframe → list

lists = df.values.tolist()
print(lists)
print(type(lists))

dataframe, series, list, dictionary, array之间互相转换

dataframe → dictionay

dictionary = df.to_dict()
print(dictionary)
print(type(dictionary))

dataframe, series, list, dictionary, array之间互相转换

Series

Series → dataframe

df_col1 = pd.DataFrame(series_col1)
print(df_col1)
print(type(df_col1))

Series → array

array_col = np.array(series_col1)
print(array_col)
print(type(array_col))

dataframe, series, list, dictionary, array之间互相转换

Series → list

list_col = list(series_col1)
print(list_col)
print(type(list_col))

dataframe, series, list, dictionary, array之间互相转换

Series → dictionary

dict_col = series_col1.to_dict()
print(dict_col)
print(type(dict_col))

dataframe, series, list, dictionary, array之间互相转换

List

list → dataframe

list_df = pd.DataFrame(lists)
print(list_df)
print(type(list_df))

dataframe, series, list, dictionary, array之间互相转换

List → Series

list_series = pd.Series(lists)
print(list_series)
print(type(list_series))

dataframe, series, list, dictionary, array之间互相转换

List → Array

list_array = np.array(lists)
print(list_array)
print(type(list_array))

dataframe, series, list, dictionary, array之间互相转换

List → Dictionary

  • 一个list
list1 = ['hello', 2, 'world', 11]
list_dict = dict(zip(list1[0::2], list1[1::2]))
print(list_dict)
print(type(list_dict))

dataframe, series, list, dictionary, array之间互相转换

  • 两个list
keys = ['col1', 'col2']
values = [1, 2]
lists_dict = dict(zip(keys, values))
print(lists_dict)
print(type(lists_dict))

dataframe, series, list, dictionary, array之间互相转换
参考:在python 中如何将 list 转化成 字典(dictionary)

Dictionary

Dictionary → Dataframe

dic = pd.DataFrame(dictionary)
print(dic)
print(type(dic))

dataframe, series, list, dictionary, array之间互相转换

Dictionary → Series

series = pd.Series(dictionary)
print(series)
print(type(series))

dataframe, series, list, dictionary, array之间互相转换

Dictionary → List

  • List of keys
keys - dictionary.keys()
#keys = list(dictionary)
print(keys)
print(type(keys))

dataframe, series, list, dictionary, array之间互相转换

  • List of values
values = dictionary.values()
print(values)
print(type(values))

dataframe, series, list, dictionary, array之间互相转换

Dictionary → Array

array = np.array(dictionary)
print(array)
print(type(array))

dataframe, series, list, dictionary, array之间互相转换

Array

Array → Dataframe

array_df = pd.DataFrame(array)
print(array_df)
print(type(array_df))

dataframe, series, list, dictionary, array之间互相转换

Array → Series

# series只能是一维的
array_series = pd.Series(array[1])
print(array_series)
print(type(array_series))

dataframe, series, list, dictionary, array之间互相转换

Array → List

array_list = list(array)
print(array_list)
print(type(array_list))

dataframe, series, list, dictionary, array之间互相转换

上一篇:MB51增强


下一篇:H3C BGP配置12-BGP扩展功能典型配置举例1-BGP LS配置举例