1.利用列表,元组构建的字典创建

import pandas as pd
dict1 = {"流浪地球":[25,45,65,89],"唐人街探案":(89,56,89,17),"谋杀2":[19,46,49,79]}
df = pd.DataFrame(dict1,index = ["第%d天"%i for i in range(1,5)])
df

2.利用series创建

import pandas as pd
import numpy as np
df = pd.DataFrame({"a":pd.Series(np.arange(1,6)),"b":pd.Series(np.linspace(1,6,5))})
df

3.利用字典构建的字典创建

pd.DataFrame({"甲":{"第一天":2,"第二天":5,"第三天":7},
            "乙":{"第一天":1,"第二天":8,"第三天":2},
             "丙":{"第一天":9,"第二天":7,"第三天":7}})

 

 4.利用二维数组进行构建

import pandas as pd
import numpy as np
arr1 = np.arange(12).reshape(3,4)
df = pd.DataFrame(arr1)
df

 5.利用字典构成的列表类构建

pd.DataFrame([{"apple":5,"banana":3,"veg":9},
              {"apple":5,"banana":3,"veg":9},
              {"apple":5,"banana":3,"veg":9}])

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐