成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

python數(shù)學(xué)建模是加深Numpy和Pandas學(xué)習(xí)

89542767 / 397人閱讀

  小編寫這篇文章的主要目的,主要是來給大家去做一個(gè)介紹的,介紹的內(nèi)容主要還是涉及到python的一些相關(guān)事情,比如我們可以利用python去進(jìn)行搭建數(shù)字建模的相關(guān)平臺(tái)。其中,主要的內(nèi)容有加深Numpy和Pandas的相關(guān)學(xué)習(xí),具體內(nèi)容,下面給大家詳細(xì)解答下。


  Numpy學(xué)習(xí)


  #Numpy的基本使用
  '''
  Numpy提供了兩種基本的對(duì)象:ndarray存儲(chǔ)單一數(shù)據(jù)類型的多維數(shù)組;
  ufunc是能夠?qū)?shù)組進(jìn)行處理的函數(shù)
  1-導(dǎo)入函數(shù)
  import numpy as np
  2-數(shù)組創(chuàng)建
  2-1 array可將列表或元組轉(zhuǎn)化為ndarray數(shù)組
  2-2 arange在給定區(qū)間內(nèi)創(chuàng)建等差數(shù)組,格式:
  arange(start=None,stop=None,step=None,dtype=None)
  【step表示步長(zhǎng)間隔】
  2-3 linspace在給定區(qū)間內(nèi)創(chuàng)建間隔相等的數(shù)組,格式:
  linspace(start,stop,num=50,endpoint=True)
  【間隔相等的num個(gè)數(shù)據(jù),其num默認(rèn)值是50】
  2-4 logspace在給定區(qū)間內(nèi)生成等比數(shù)組,格式:
  logspace(start,stop,num=50,endpoint=True,base=10.0)
  【默認(rèn)生成區(qū)間[10start(次方),10stop()次方]上的num個(gè)數(shù)據(jù)的等比數(shù)組】
  以及ones、zeros、empty和ones_like等系列函數(shù)的運(yùn)用:
  '''

  1-numpy.array


  #numpy.array
  #array()函數(shù),括號(hào)內(nèi)可以是列表、元組、數(shù)組、迭代對(duì)象、生成器
  import numpy as np
  print(np.array([6,6,6]))#列表
  print(np.array((8,8,8)))#元組
  print(np.array(np.array([9,9,9])))#數(shù)組
  print(np.array(range(10)))#迭代對(duì)象/整型
  print(np.array([i**2 for i in range(10)]))#生成器
  #創(chuàng)建10以內(nèi)的奇數(shù)的數(shù)組:
  print(np.array([i for i in range(1,10,2)]))
  print(np.array([i for i in range(10)if i%2!=0]))
  #創(chuàng)建10以內(nèi)的偶數(shù)的數(shù)組:
  print(np.array([i for i in range(0,10,2)]))
  print(np.array([i for i in range(10)if i%2==0]))
  #列表中元素類型不相同
  print(np.array([5,2,'0']))#['5''2''0']
  #浮點(diǎn)型
  print(np.array([3,4,5.2]))
  #二維數(shù)組:【嵌套序列(列表、元組均可)】
  print(np.array([[6,7,8],('lxw','cw','wl')]))
  print(np.array([[6,7,8],('lxw','cw','wl')]).ndim)#ndim(維度):2
  #嵌套數(shù)量不一致:【強(qiáng)制轉(zhuǎn)化為一維,推薦不用】
  print(np.array([[6,7,8],('lxw','cw','wl','npy')],dtype=object))
  print(np.array([[6,7,8],('lxw','cw','wl','npy')],dtype=object).ndim)#ndim(維度):1
  print(np.array([[6,7,8],('lxw','cw','wl','npy')],dtype=object).shape)#運(yùn)行結(jié)果:(2,)
  print(np.array([[6,7,8],[9,9,6,9]],dtype=object))
  print(np.array([[6,7,8],[9,9,6,9]],dtype=object).ndim)#ndim(維度):1
  print(np.array([[6,7,8],[9,9,6,9]],dtype=object).shape)#運(yùn)行結(jié)果:(2,)->代表兩行一列


  2-numpy.empty


  #numpy.empty
  '''
  numpy.empty方法用來創(chuàng)建一個(gè)指定形狀(shape)、數(shù)據(jù)類型(dtype)且未初始化的數(shù)組
  numpy.empty(shape,dtype=float,order='C')
  參數(shù)說明:
  參數(shù)描述
  shape數(shù)組形狀
  dtype數(shù)據(jù)類型,可選
  order有"C"和"F"兩個(gè)選項(xiàng),分別代表,行優(yōu)先和列優(yōu)先,在計(jì)算機(jī)內(nèi)存中的存儲(chǔ)元素的順序
  '''
  import numpy as np
  lxw=np.empty([3,4],dtype=int)
  print(lxw)#注意:數(shù)組元素為隨機(jī)值,因?yàn)樗鼈兾闯跏蓟?/pre>

  3-numpy.zeros


  #numpy.zeros
  '''
  創(chuàng)建指定大小的數(shù)組,數(shù)組元素以0來填充:
  numpy.zeros(shape,dtype=float,order='C')
  參數(shù)說明:
  order:'C'用于C的行數(shù)組,或者'F'用于FORTRAN的列數(shù)組
  '''
  import numpy as np
  lxw=np.zeros(6)#默認(rèn)為浮點(diǎn)數(shù)
  print(lxw)
  lxw2=np.zeros((6,),dtype=int)#設(shè)置類型為整數(shù)
  print(lxw2)
  #自定義類型
  lxw3=np.zeros((2,2),dtype=[('lxw','i2'),('lxw2','i4')])
  print(lxw3)


  4-numpy.ones


  #numpy.ones
  '''創(chuàng)建指定形狀的數(shù)組,數(shù)組元素以1來填充:
  numpy.ones(shape,dtype=None,order='C')
  '''
  import numpy as np
  lxw4=np.ones(8)#默認(rèn)浮點(diǎn)數(shù)
  print(lxw4)
  lxw5=np.ones([2,2],dtype=int)
  print(lxw5)


  NumPy從已有的數(shù)組創(chuàng)建數(shù)組


  1-numpy.asarray


  #numpy.asarray
  '''
  numpy.asarray類似numpy.array,但numpy.asarray參數(shù)只有三個(gè),比numpy.array少兩個(gè)。
  numpy.asarray(a,dtype=None,order=None)
  參數(shù)說明:
  參數(shù)描述
  a任意形式的輸入?yún)?shù),可以是,列表,列表的元組,元組,元組的元組,元組的列表,多維數(shù)組
  '''
  #將列表轉(zhuǎn)換為ndarray:
  import numpy as np
  x=[5,2,0]
  lxw6=np.asarray(x)
  print(lxw6)
  #將元組轉(zhuǎn)換為ndarray
  import numpy as np
  x2=(1,3,1,4)
  lxw7=np.asarray(x2)
  print(lxw7)
  #設(shè)置了dtype參數(shù)
  import numpy as np
  x4=[6,6,9]
  lxw9=np.asarray(x4,dtype=float)
  print(lxw9)

  2-numpy.frombuffer


  #numpy.frombuffer
  '''
  numpy.frombuffer用于實(shí)現(xiàn)動(dòng)態(tài)數(shù)組;接受buffer輸入?yún)?shù),以流的形式讀入轉(zhuǎn)化成ndarray對(duì)象。
  格式如下:
  numpy.frombuffer(buffer,dtype=float,count=-1,offset=0)
  注:buffer是字符串的時(shí)候,Python3默認(rèn)str是Unicode類型,所以要轉(zhuǎn)成bytestring在原str前加上b。
  參數(shù)說明:
  參數(shù)描述
  buffer可以是任意對(duì)象,會(huì)以流的形式讀入。
  dtype返回?cái)?shù)組的數(shù)據(jù)類型,可選
  count讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù)。
  offset讀取的起始位置,默認(rèn)為0
  '''
  import numpy as np
  s=b'lxw_pro'
  lxw10=np.frombuffer(s,dtype='S1')
  print(lxw10)

  3-numpy.fromiter


  #numpy.fromiter
  '''
  numpy.fromiter方法從可迭代對(duì)象中建立ndarray對(duì)象,返回一維數(shù)組。
  numpy.fromiter(iterable,dtype,count=-1)
  '''
  import numpy as np
  lst=range(6)
  it=iter(lst)
  lxw11=np.fromiter(it,dtype=float)
  print(lxw11)


  NumPy從數(shù)值范圍創(chuàng)建數(shù)組


  1-numpy.arange


  #numpy.arange
  '''
  numpy包中的使用arange函數(shù)創(chuàng)建數(shù)值范圍并返回ndarray對(duì)象,函數(shù)格式如下:
  numpy.arange(start,stop,step,dtype)
  根據(jù)start與stop指定的范圍以及step設(shè)定的步長(zhǎng),生成一個(gè)ndarray。
  參數(shù)說明:
  參數(shù)描述
  start起始值,默認(rèn)為0
  stop終止值(不包含)
  step步長(zhǎng),默認(rèn)為1
  dtype返回ndarray的數(shù)據(jù)類型,如果沒有提供,則會(huì)使用輸入數(shù)據(jù)的類型
  '''
  #生成0和5的數(shù)組
  import numpy as np
  a=np.arange(6)
  print(a)
  #設(shè)置返回類型位float
  import numpy as np
  a2=np.arange(6,dtype=float)
  print(a2)
  #設(shè)置了起始值、終止值及步長(zhǎng)
  import numpy as np
  a3=np.arange(20,52,5)
  print(a3)

  2-numpy.linspace


  #numpy.linspace
  '''
  numpy.linspace函數(shù)用于創(chuàng)建一個(gè)一維數(shù)組,數(shù)組是一個(gè)等差數(shù)列構(gòu)成的,格式如下:
  np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)
  參數(shù)說明:
  參數(shù)描述
  start序列的起始值
  stop序列的終止值,如果endpoint為true,該值包含于數(shù)列中
  num要生成的等步長(zhǎng)的樣本數(shù)量,默認(rèn)為50
  endpoint該值為true時(shí),數(shù)列中包含stop值,反之不包含,默認(rèn)是True。
  retstep如果為True時(shí),生成的數(shù)組中會(huì)顯示間距,反之不顯示。
  dtype ndarray的數(shù)據(jù)類型
  '''
  #類似等差數(shù)列
  import numpy as np
  a4=np.linspace(1,10,5)
  print(a4)
  #設(shè)置元素全部是1的等差數(shù)列
  import numpy as np
  a5=np.linspace(1,1,10)
  print(a5)
  #將endpoint設(shè)為false,不包含終止值
  import numpy as np
  a6=np.linspace(8,22,4,endpoint=False)
  print(a6)
  #注:將endpoint設(shè)為true,則會(huì)包含22
  a6=np.linspace(8,22,4,endpoint=True)
  print(a6)
  #設(shè)置間距
  import numpy as np
  a7=np.linspace(5,10,5).reshape([5,1])
  print(a7)


  3-numpy.logspace


  #numpy.logspace
  '''
  numpy.logspace函數(shù)用于創(chuàng)建一個(gè)于等比數(shù)列。格式如下:
  np.logspace(start,stop,num=50,endpoint=True,base=10.0,dtype=None)
  base參數(shù)意思是取對(duì)數(shù)的時(shí)候log的下標(biāo)。
  參數(shù)描述
  start序列的起始值為:base**start
  stop序列的終止值為:base**stop。如果endpoint為true,該值包含于數(shù)列中
  num要生成的等步長(zhǎng)的樣本數(shù)量,默認(rèn)為50
  endpoint該值為true時(shí),數(shù)列中中包含stop值,反之不包含,默認(rèn)是True。
  base對(duì)數(shù)log的底數(shù)。
  dtype ndarray的數(shù)據(jù)類型
  '''
  import numpy as np
  a8=np.logspace(1,2,num=10)#默認(rèn)底數(shù)是10
  print(a8)
  #將對(duì)數(shù)的底數(shù)設(shè)置為2
  import numpy as np
  a9=np.logspace(0,8,9,base=2)
  print(a9)

  綜合運(yùn)用【array、arange、linspace、lonspace】:


  #綜合運(yùn)用
  import numpy as np
  ltw=np.array([3,3,4,4])#生成整型數(shù)組
  ltw2=ltw.astype(float)#轉(zhuǎn)為浮點(diǎn)數(shù)
  ltw3=np.array([5,2,1],dtype=float)#浮點(diǎn)數(shù)
  print(ltw)
  print(ltw2)
  print(ltw3)
  #比較類型
  print(ltw.dtype,ltw2.dtype,ltw3.dtype)
  aa=np.array([
  [2,5,8],
  [9,6,2]
  ])
  print(aa)
  bb=np.arange(2,9)
  print(bb)#運(yùn)行結(jié)果為:[2 3 4 5 6 7 8]
  cc=np.linspace(2,5,4)
  print(cc)#運(yùn)行結(jié)果為:[2.3.4.5.]
  dd=np.logspace(1,4,4,base=2)#base控制的是幾次方
  print(dd)#運(yùn)行結(jié)果為:[2.4.8.16.]


  綜合運(yùn)用【ones、zeros、empty、ones_like】


  #綜合運(yùn)用【ones、zeros、empty、ones_like】
  import numpy as np
  a=np.ones(6,dtype=int)
  print(a)#運(yùn)行結(jié)果為:[1 1 1 1 1 1]
  b=np.ones((6,),dtype=int)
  print(b)#運(yùn)行結(jié)果為:[1 1 1 1 1 1]
  c=np.ones((3,1))
  print(c)#輸出3行一列的數(shù)組
  #運(yùn)行結(jié)果為:
  #[[1.]
  #[1.]
  #[1.]]
  d=np.zeros(4)
  print(d)#運(yùn)行結(jié)果為:[0.0.0.0.]
  e=np.empty(3)
  print(e)#生成3個(gè)元素的空數(shù)組行向量
  #運(yùn)行結(jié)果為:[1.1.1.]
  f=np.eye(3)
  print(f)#生成3階單位陣
  #運(yùn)行結(jié)果為:
  #[[1.0.0.]
  #[0.1.0.]
  #[0.0.1.]]
  g=np.eye(3,k=1)
  print(g)#生成第k對(duì)角線的元素為1,其他元素為0的3階方陣
  #運(yùn)行結(jié)果為:
  #[[0.1.0.]
  #[0.0.1.]
  #[0.0.0.]]
  h=np.zeros_like(b)
  print(h)#生成與a同維數(shù)的全0數(shù)組
  #運(yùn)行結(jié)果為:[0 0 0 0 0 0]


  1.NumPy切片和索引


  #NumPy切片和索引
  '''
  ndarray對(duì)象的內(nèi)容可以通過索引或切片來訪問和修改,與Python中l(wèi)ist的切片操作一樣。
  ndarray數(shù)組可以基于0-n的下標(biāo)進(jìn)行索引,
  切片對(duì)象可以通過內(nèi)置的slice函數(shù),并設(shè)置start,stop及step參數(shù)進(jìn)行,從原數(shù)組中切割出一個(gè)新數(shù)組
  '''
  import numpy as np
  #通過arange()函數(shù)創(chuàng)建ndarray對(duì)象
  a=np.arange(10)
  lxw=slice(2,9,3)#索引從2到9,間隔為3
  print(a[lxw])#[2 5 8]
  #通過切片操作
  a=np.arange(10)
  lxw2=a[2:9:3]#這里的切片操作和Python中l(wèi)ist的操作是一樣的
  print(lxw2)#[2 5 8]
  #比如:
  import numpy as np
  lxw3=np.arange(10)
  print(lxw3[6])#6
  print(lxw3[6:])#[6 7 8 9]
  print(lxw3[2:7])#[2 3 4 5 6]
  #多維數(shù)組同樣適用上述索引提取方法
  import numpy as np
  lxw4=np.array([
  [6,6,6],
  [5,2,0],
  [5,8,9]
  ])
  print(lxw4)
  print(lxw4[1:])
  #切片還可以包括省略號(hào)…,來使選擇元組的長(zhǎng)度與數(shù)組的維度相同。
  #如果在行位置使用省略號(hào),它將返回包含行中元素的ndarray
  import numpy as np
  lxw5=np.array([
  [1,2,9],
  [2,5,4],
  [3,4,8]
  ])
  print(lxw5[1,...])#[2 5 4]第二行元素
  print(lxw5[...,2])#[9 4 8]第三列元素
  print(lxw5[1:,...])#第二行及剩下元素
  print(lxw5[...,1:])#第二列及剩下元素

  NumPy高級(jí)索引


  Numpy中的array數(shù)組與Python基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)列表(list)的區(qū)別是:


  列表中的元素可以是不同的數(shù)據(jù)類型array數(shù)組只允許存儲(chǔ)相同的數(shù)據(jù)類型


  NumPy比一般的Python序列提供更多的索引方式。


  除了之前看到的用整數(shù)和切片的索引外,數(shù)組可以由


  整數(shù)數(shù)組索引布爾索引花式索引


  1-整數(shù)數(shù)組索引


  #1-整數(shù)數(shù)組索引
  import numpy as np
  b=np.array([
  [6,2,9],
  [4,3,9],
  [5,2,3]
  ])
  lxw6=b[
  [0,1,2],[1,2,1]
  ]
  print(lxw6)#輸出[2 9 2]
  #獲取四個(gè)角元素
  import numpy as np
  aq=np.array([
  [1,2,3,4],
  [2,3,4,5],
  [3,4,5,6],
  [4,5,6,7]
  ])
  print(aq)
  hj=np.array([[0,0],[3,3]])
  lj=np.array([[0,3],[0,3]])
  yq=aq[hj,lj]
  print(yq)
  print()
  #可借助切片:或…與索引數(shù)組組合:
  import numpy as np
  jz=np.array([
  [3,5,9],
  [5,2,6],
  [2,9,8]
  ])
  jz1=jz[:2,:2]
  print(jz1)
  jz2=jz[:2,[0,1]]
  print(jz2)
  jz3=jz[...,1:]
  print(jz3)


  2-布爾索引


  #布爾索引
  #布爾索引可通過布爾運(yùn)算(如:比較運(yùn)算符)來獲取符合指定條件的元素的數(shù)組
  #獲取大于5的元素:
  import numpy as np
  br=np.array([
  [6,7,8],
  [5,2,1],
  [6,6,9],
  [2,4,5]
  ])
  print(br)
  print(br[br>5])#輸出[6 7 8 6 6 9]
  #使用~(取補(bǔ)運(yùn)算符)來過濾NaN:
  import numpy as np
  bu=np.array([5,np.nan,2,0,np.nan,np.nan,5,8])
  print(bu[~np.isnan(bu)])#輸出[5.2.0.5.8.]
  #從數(shù)組中過濾掉非復(fù)數(shù)元素:
  import numpy as np
  lv=np.array([2+2.9j,4,9,2+8.2j,8])
  print(lv[np.iscomplex(lv)])#輸出[2.+2.9j 2.+8.2j]


  3-花式索引


  #花式索引【利用整數(shù)數(shù)組進(jìn)行索引】
  #花式索引根據(jù)索引數(shù)組的值作為目標(biāo)數(shù)組的某個(gè)軸的下標(biāo)來取值。
  #對(duì)于使用一維整型數(shù)組作為索引,如果目標(biāo)是一維數(shù)組,那么索引的結(jié)果就是對(duì)應(yīng)下標(biāo)的行,
  #如果目標(biāo)是二維數(shù)組,那么就是對(duì)應(yīng)位置的元素。
  #注:花式索引跟切片不一樣,它總是將數(shù)據(jù)復(fù)制到新數(shù)組中。
  #1.傳入順序索引數(shù)組
  import numpy as np
  sx=np.arange(32).reshape(8,4)
  print(sx[[5,2,1,6]])
  #2.傳入倒序索引數(shù)組
  import numpy as np
  dx=np.arange(32).reshape(8,4)
  print(dx[[-5,-2,-1,-6]])
  #3.傳入多個(gè)索引數(shù)組(要使用np.ix_)
  import numpy as np
  dg=np.arange(32).reshape(8,4)
  print(dg[np.ix_([2,3,5,1],[3,2,0,1])])

  三個(gè)實(shí)用小方法:


  條件加小括號(hào)


  使用np.logical_and方法


  使用np.all方法


  import numpy as np
  sy=np.array([
  [3,5,6],
  [2,6,2],
  [5,2,0],
  [3,3,4]
  ])
  #原數(shù)組
  print(sy)
  #1-
  print(sy[(sy>3)&(sy<6)])#條件記得加小括號(hào)
  #2-
  print(sy[np.logical_and(sy>3,sy<6)])
  #3-
  print(sy[np.all([sy>3,sy<6],axis=0)])

  綜合運(yùn)用【數(shù)組元素的索引】


  相關(guān)代碼如下:


  import numpy as np
  x=np.arange(16).reshape(4,4)
  print(x)#生成4行4列的數(shù)組
  x2=x[2][1]
  print(x2)#輸出9
  x3=x[2,1]
  print(x3)#輸出9
  x4=x[1:2,2:4]
  print(x4)#輸出[[6 7]]
  xx=np.array([0,1,2,1])
  print(x[xx==1])#輸出x的第2、4行元素


  Pandas學(xué)習(xí)(續(xù))


  #Pandas學(xué)習(xí)(續(xù))
  #Pandas庫是在Numpy庫基礎(chǔ)上開發(fā)的一種數(shù)據(jù)分析工具
  '''
  Pandas主要提供了三種數(shù)據(jù)結(jié)構(gòu):
  1-Series:帶標(biāo)簽的一維數(shù)組
  2-DataFrame:帶標(biāo)簽且大小可變得二維表格結(jié)構(gòu)
  3-Panel:帶標(biāo)簽且大小可變得三維數(shù)組
  '''
  #生成二維數(shù)組
  #生成服從標(biāo)準(zhǔn)正態(tài)分布的24*4隨機(jī)數(shù)矩陣,并保存為DataFrame數(shù)據(jù)結(jié)構(gòu)。
  import pandas as pd
  import numpy as np
  dates=pd.date_range(start='20220622',end='20220707',freq='D')
  print(dates)

  運(yùn)行效果如下:

01.png

  lxw1=pd.DataFrame(np.random.randn(16,4),index=dates,columns=list('ABCD'))
  lxw2=pd.DataFrame(np.random.randn(16,4))
  print(lxw1)
  print(lxw2)


  運(yùn)行結(jié)果如下:

02.png

03.png

  1將數(shù)據(jù)寫入excel、csv文件


  #將lxw1的數(shù)據(jù)寫入excel文件
  lxw1.to_excel('假期培訓(xùn)時(shí)間.xlsx')
  lxw1.to_excel("時(shí)間任意.xlsx",index=False)#不包含行索引
  #將lxw2的數(shù)據(jù)寫入csv文件
  lxw2.to_csv('假期培訓(xùn)時(shí)間.csv')
  lxw2.to_csv("時(shí)間隨意.csv",index=False)#不包含行索引
  #創(chuàng)建文件對(duì)象
  f=pd.ExcelWriter('培訓(xùn)時(shí)間(格式).xlsx')
  #把lxw1寫入Excel文件
  lxw1.to_excel(f,"Shell1")
  #把lxw2寫入Excel文件
  lxw2.to_excel(f,"Sheet2")
  f.save()

  

04.png

05.png

  #從文件中讀入數(shù)據(jù):
  import pandas as pd
  lxw3=pd.read_csv("假期培訓(xùn)時(shí)間.csv",usecols=range(1,4))
  print(lxw3)


  運(yùn)行結(jié)果如下:

06.png

  lxw4=pd.read_excel("培訓(xùn)時(shí)間(格式).xlsx","Sheet2",usecols=range(1,3))
  print(lxw4)
  2數(shù)據(jù)的一些預(yù)處理
  #數(shù)據(jù)的一些預(yù)處理
  #DataFrame數(shù)據(jù)的拆分、合并和分組計(jì)算:
  import pandas as pd
  import numpy as np
  lxw5=pd.DataFrame(np.random.randint(1,6,(10,4)),columns=list('ABCD'))
  print(lxw5)
  lxww=lxw5[:5]#獲取前五行數(shù)據(jù)
  print(lxww)
  wy=pd.concat([lxww,lxwy])#數(shù)據(jù)行合并
  print(wy)
  q1=lxw5.groupby('A').mean()#數(shù)據(jù)分組求均值
  print(np.around(q1,decimals=2))#decimals表示保留幾位小數(shù)
  q2=lxw5.groupby('A').apply(sum)#數(shù)據(jù)分組求和
  print(q2)

07.png

08.png

  3數(shù)據(jù)的選取與操作


  #數(shù)據(jù)的選取與操作
  '''
  對(duì)DataFrame進(jìn)行選取,要從3個(gè)層次考慮:行列、區(qū)域、單元格
  1-選用中括號(hào)[]選取行列
  2-使用行和列的名稱進(jìn)行標(biāo)簽定位的df.loc[]
  3-使用整型索引(絕對(duì)位置索引)的df.iloc[]
  當(dāng)然,在數(shù)據(jù)預(yù)處理中,需要對(duì)缺失值等進(jìn)行一些特殊處理
  '''
  #數(shù)據(jù)操作:
  import pandas as pd
  import numpy as np
  qq=pd.DataFrame(np.random.randint(1,5,(6,4)),
  index=['a','b','c','d','e','f'],
  columns=['one','two','three','four'])
  qq.loc['c','two']=np.nan#修改第三行第二列的數(shù)據(jù)
  print(qq)

11.png

  ww=qq.iloc[1:4,0:2]#提取第二、三、四行,第一、二列數(shù)據(jù)
  print(ww)

12.png

  qq['five']='lxw'#增加第五列數(shù)據(jù)
  print(qq)

13.png

  qq2=qq.reindex(['a','b','c','d','e','f','g'])#增加行名
  print(qq2)

14.png

  qq3=qq2.dropna()#刪除有不確定值的行


  print(qq3)#從輸出不難看出,刪除了c行和g行


  到的問題:


  1-代碼運(yùn)行錯(cuò)誤是很正常的事,只要自己能解決,那遲早也是對(duì)的,是吧!每次運(yùn)行錯(cuò)誤,我都會(huì)自己先找找原因,要么多看幾眼代碼,要么直接復(fù)制運(yùn)行報(bào)錯(cuò)的代碼,去百度翻譯自己查查是什么意思,在結(jié)合意思查詢相關(guān)資料以修正代碼!


  2-后面再去看看【模型與算法】,發(fā)現(xiàn)自己所存儲(chǔ)的知識(shí)不夠,所以還得繼續(xù)學(xué)習(xí)新的知識(shí),一次一次地突破!


  總結(jié):


  面臨著一次次的運(yùn)行錯(cuò)誤,一次又一次的解決,或許解決的難題越多,你懂的就會(huì)越來越多吧,就如同你經(jīng)歷的一樣,你經(jīng)歷的越多,知道的就越多!

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/128409.html

相關(guān)文章

  • 如何入門機(jī)器學(xué)習(xí)的呢

    摘要:在這里我分享下我個(gè)人入門機(jī)器學(xué)習(xí)的經(jīng)歷,希望能對(duì)大家能有所幫助。相關(guān)學(xué)習(xí)鏈接,,入門后的體驗(yàn)在入門了機(jī)器學(xué)習(xí)之后,在實(shí)際工作中,絕大多數(shù)的情況下你并不需要去創(chuàng)造一個(gè)新的算法。 機(jī)器學(xué)習(xí)在很多眼里就是香餑餑,因?yàn)闄C(jī)器學(xué)習(xí)相關(guān)的崗位在當(dāng)前市場(chǎng)待遇不錯(cuò),但同時(shí)機(jī)器學(xué)習(xí)在很多人面前又是一座大山,因?yàn)榘l(fā)現(xiàn)它太難學(xué)了。在這里我分享下我個(gè)人入門機(jī)器學(xué)習(xí)的經(jīng)歷,希望能對(duì)大家能有所幫助。 PS:這篇文章...

    ShowerSun 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<