pandas list\dict 转换为DataFrame
目录
一、list 转为 DataFrame
1、一维数组
import pandas as pd a = [1,2,3,4] df = pd.DataFrame(a, columns=['num']) print(df)
结果展示:
2、二维数组 list of list
import pandas as pd a = [[1,2,3,4],[5,6,7,8]] df = pd.DataFrame(a) print(df)
结果展示:
3、行列转换
import pandas as pd People_List = [['Jon','Mark','Maria','Jill','Jack'],['Smith','Brown','Lee','Jones','Ford'],[21,38,42,28,55]] df = pd.DataFrame (People_List).transpose() df.columns = ['First_Name','Last_Name','Age'] print (df)
结果展示:
二、dict 转为 DataFrame
1、dict of list
import pandas as pd dict = {"l1":[1,2,3,4],"l2":[5,6,7,8]} df = pd.DataFrame(dict) print(df)
结果展示: