溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

對(duì)pandas的算術(shù)運(yùn)算和數(shù)據(jù)對(duì)齊實(shí)例詳解

發(fā)布時(shí)間:2020-09-28 05:16:53 來源:腳本之家 閱讀:171 作者:修煉之路 欄目:開發(fā)技術(shù)

pandas可以對(duì)不同索引的對(duì)象進(jìn)行算術(shù)運(yùn)算,如果存在不同的索引對(duì),結(jié)果的索引就是該索引對(duì)的并集。

一、算術(shù)運(yùn)算

a、series的加法運(yùn)算

  s1 = Series([1,2,3],index=["a","b","c"])
  s2 = Series([4,5,6],index=["a","c","e"])
  print(s1+s2)
  '''
  a  5.0
  b  NaN
  c  8.0
  e  NaN
  '''

sereis相加會(huì)自動(dòng)進(jìn)行數(shù)據(jù)對(duì)齊操作,在不重疊的索引處會(huì)使用NA(NaN)值進(jìn)行填充,series進(jìn)行算術(shù)運(yùn)算的時(shí)候,不需要保證series的大小一致。

b、DataFrame的加法運(yùn)算


  d1 = np.arange(1,10).reshape(3,3)
  dataFrame1 = DataFrame(d1,index=["a","b","c"],columns=["one","two","three"])
  d2 = np.arange(1,10).reshape(3,3)
  dataFrame2 = DataFrame(d2,index=["a","b","e"],columns=["one","two","four"])
  print(dataFrame1+dataFrame2)
  '''
    four one three  two
  a  NaN 2.0  NaN  4.0
  b  NaN 8.0  NaN 10.0
  c  NaN NaN  NaN  NaN
  e  NaN NaN  NaN  NaN
  '''

dataFrame相加時(shí),對(duì)齊操作需要行和列的索引都重疊的時(shí)候才回相加,否則會(huì)使用NA值進(jìn)行填充。

二、指定填充值

  s1 = Series([1,2,3],index=["a","b","c"])
  s2 = Series([4,5,6],index=["a","c","e"])
  print( s1.add(s2,fill_value=0))
  '''
  a  5.0
  b  2.0
  c  8.0
  e  6.0
  '''

需要注意的時(shí)候,使用add方法對(duì)兩個(gè)series進(jìn)行相加的時(shí)候,設(shè)置fill_value的值是對(duì)于不存在索引的series用指定值進(jìn)行填充后再進(jìn)行相加。除了加法add,還有sub減法,div除法,mul乘法,使用方式與add相同。DataFrame與series一樣。

  s1 = Series([1,2,3],index=["a","b","c"])
  s2 = Series([4,5,6],index=["a","c","e"])
  print(s2.reindex(["a","b","c","d"],fill_value=0))
  '''
  a  4
  b  0
  c  5
  d  0
  '''
  s3 = s1 + s2
  print(s3.reindex(["a","b","c","e"],fill_value=0))
  '''
  a  5.0
  b  NaN
  c  8.0
  e  NaN
  '''

使用reindex進(jìn)行填充的時(shí)候,需要注意的是,不能對(duì)已經(jīng)是值為NaN的進(jìn)行重新賦值,只能對(duì)使用reindex之前不存在的所以使用指定的填充值,DataFrame也是一樣的。

三、DataFrame與Series的混合運(yùn)算

a、DataFrame的行進(jìn)行廣播

  a = np.arange(9).reshape(3,3)
  d = DataFrame(a,index=["a","b","c"],columns=["one","two","three"])
  #取d的第一行為Series
  s = d.ix[0]
  print(d+s)
  '''
    one two three
  a  0  2   4
  b  3  5   7
  c  6  8   10
  '''

b、DataFrame的列進(jìn)行廣播

  a = np.arange(9).reshape(3,3)
  d = DataFrame(a,index=["a","b","c"],columns=["one","two","three"])
  #取d的第一列為Series
  s = d["one"]
  print(d.add(s,axis=0))
  '''
    one two three
  a  0  1   2
  b  6  7   8
  c  12  13   14
  '''

對(duì)列進(jìn)行廣播的時(shí)候,必須要使用add方法,而且還要將axis設(shè)置為0,不然就會(huì)得到下面的結(jié)果

  print(d.add(s))
  '''
    a  b  c one three two
  a NaN NaN NaN NaN  NaN NaN
  b NaN NaN NaN NaN  NaN NaN
  c NaN NaN NaN NaN  NaN NaN
  '''

以上這篇對(duì)pandas的算術(shù)運(yùn)算和數(shù)據(jù)對(duì)齊實(shí)例詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI