import numpy as np import pandas as pd # 序列的创建,总共三种方式 #(1)一维数组创建 arr = np.arange(10) print(arr) print(type(arr)) s1 = pd.Series(arr) print(s1) print(type(s1)) print(s1[1]) #(2)字典的方式创建 dic = {‘a‘:10,‘b‘:20,‘c‘:30,‘d‘:40,‘e‘:50} print(dic) print(type(dic)) s2 = pd.Series(dic) print(s2) print(type(s2)) #通过DataFrame中的某一行或某一列创建序列(后续说) #DateFrame的创建,三种方式 #(1)通过二维数组创建数据框 arr2 = np.array(np.arange(12)).reshape(4,3) print(arr2) print(type(arr2)) df = pd.DataFrame(arr2) print(df) print(type(df)) #(2)通过字典的方式创建数据框 dic1 = {‘a‘:[1,2,3,4],‘b‘:[5,6,7,8],‘c‘:[9,10,11,12],‘d‘:[13,14,15,16]} print(dic1) print(type(dic1)) df1=pd.DataFrame(dic1) print(df1) print(type(dic1)) #(3)通过嵌套字典的方式创建数据框 dic2={‘a‘:{‘a1‘:[1,2,3,4],‘b‘:[5,6,7,8]},‘b‘:{‘a2‘:[2,3,4,5],‘b2‘:[6,7,8,9]},‘c‘:{‘a3‘:[0,1,2,3],‘b3‘:[0,7,6,5]}} print(dic2) print(type(dic2)) df2 = pd.DataFrame(dic2) print(df2) print(type(df2)) #(4)通过数据框的方式创建数据框 df3 = df2[[‘a‘,‘b‘]] print(df3) print(type(df3))
原文地址:https://www.cnblogs.com/lyr999736/p/10016024.html
时间: 2024-10-18 12:05:20