Series的创建
#字典创建 a={'a':1 ,'b':2 , 'c':3} S=pd.Series(a) #数组创建 S1=pd.Series(np.random.randn(4)) #用标量创建 S2=pd.Series(10,index=range(4)) #标量的个数由idnex的个数决定 复制代码
Series的索引
s = pd.Series(np.random.rand(5)*100, index = ['a','b','c','d','e']) print(s[['a','c','e']])#选取自己想要的值 print(s[1:3])#左闭右开 print(s['a':'c'])#都是闭区间 print(s[-1])#倒过来 print(s[::2])#步长为2 # 布尔索引 bs1=s>50 bs2=s.isnull() bs3=s.notnull() S2=s[s.notnull()]#输出结果是输出S2不是null的值 # 布尔索引的作用可以用于筛选,返回的是布尔类型 复制代码
Series的常用函数
s = pd.Series(np.random.rand(3), index = ['a','b','c']) s.head()#查看前五条数据 s.tail()#查看后五条 s1=s.reindex(['b','a','c','d'],fill_value=0)#reindex新加的索引行为空,fill_value参数是把空值填充为0 复制代码
Series的自动对齐
s1 = pd.Series(np.random.rand(3), index = ['Jack','Marry','Tom']) s2 = pd.Series(np.random.rand(3), index = ['Wang','Jack','Marry']) print(s1+s2) 复制代码
#python会自耦东识别相同的标签相加,没有相同标签的或者值为空值的相加后为null
Series的增删改
s1 = pd.Series(np.random.rand(5)) s2 = pd.Series(np.random.rand(5), index = list('ngjur')) #增 s2['a']=100 s1[5]=30 #删 s1.drop(1) s2.drop('n') #改 s2['a']=1 复制代码
DataFrame的五种创建方式
#方法一 由list组成的字典 data={'a':[1,2,3], 'b':np.random.rand(3) } df=pd.DataFrame(data,index=['one','two','three']) #注意index的个数必须与行数相等,columns的个数可以任意,多出来的系统会默认是空值 #方法二由Series组成的字典生成 data={'one':np.Series(np.random.rand(3)), 'two':np.random.rand(2) } df=pd.DateFrame(data