5. 연산
페이지 정보
작성자 관리자 댓글 0건 조회 3,107회 작성일 20-01-23 20:12본문
5. 연산
색인이 다른 객체를 더하는 산술연산
 
실습1.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
s1 = Series([5,6,-1,2], index=['a','c','d','e'])
s2 = Series([3,4,-1,2,7], index=['a','c','e','f','g'])
print(s1+s2)
[결과]
a     8.0
c    10.0
d     NaN
e     1.0
f     NaN
g     NaN
dtype: float64
실습2.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df1=DataFrame(np.arange(9).reshape(3,3), columns=list('bcd'),
  index=['seoul', 'busan', 'kwangju'])
df2=DataFrame(np.arange(12).reshape(4,3), columns=list('bde'),
  index=['incheon','seoul','busan','suwon'])
print(df1)
print(df2)
print(df1+df2)
[결과]
         b  c  d
seoul    0  1  2
busan    3  4  5
kwangju  6  7  8
         b   d   e
incheon  0   1   2
seoul    3   4   5
busan    6   7   8
suwon    9  10  11
           b   c     d   e
busan    9.0 NaN  12.0 NaN
incheon  NaN NaN   NaN NaN
kwangju  NaN NaN   NaN NaN
seoul    3.0 NaN   6.0 NaN
suwon    NaN NaN   NaN NaN
실습3.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df3 = DataFrame(np.arange(12).reshape(3,4), columns=list('abcd'))
df4 = DataFrame(np.arange(20).reshape(4,5), columns=list('abcde'))
print(df3)
print(df4)
print(df3+df4)
print(df3.add(df4, fill_value=0))
print(df3.reindex(columns=df4.columns, fill_value=0))
[결과]
   a  b   c   d
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
    a   b   c   d   e
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19
      a     b     c     d   e
0   0.0   2.0   4.0   6.0 NaN
1   9.0  11.0  13.0  15.0 NaN
2  18.0  20.0  22.0  24.0 NaN
3   NaN   NaN   NaN   NaN NaN
      a     b     c     d     e
0   0.0   2.0   4.0   6.0   4.0
1   9.0  11.0  13.0  15.0   9.0
2  18.0  20.0  22.0  24.0  14.0
3  15.0  16.0  17.0  18.0  19.0
   a  b   c   d  e
0  0  1   2   3  0
1  4  5   6   7  0
2  8  9  10  11  0
DataFrame과 Series간의 연산
(Numpy의 브로드캐스팅과 유사하다.)
실습4.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
arr = np.arange(12.).reshape(3,4)
print(arr)
print(arr[0])
print(arr - arr[0])
[결과]
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]]
[0. 1. 2. 3.]
[[0. 0. 0. 0.]
 [4. 4. 4. 4.]
 [8. 8. 8. 8.]]
실습5.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.arange(12).reshape(4,3), columns=list('bde'),
 index=['seoul', 'kwangju', 'daegu', 'incheon'])
print(df)
s1 = df.iloc[0]
print(s1)
print(df-s1)
[결과]
         b   d   e
seoul    0   1   2
kwangju  3   4   5
daegu    6   7   8
incheon  9  10  11
b    0
d    1
e    2
Name: seoul, dtype: int32
         b  d  e
seoul    0  0  0
kwangju  3  3  3
daegu    6  6  6
incheon  9  9  9
실습6.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.arange(12).reshape(4,3), columns=list('bde'),
 index=['seoul', 'kwangju', 'daegu', 'incheon'])
print(df)
s2 = Series(range(3), index=list('bef'))
print(s2)
print(df+s2)
[결과]
         b   d   e
seoul    0   1   2
kwangju  3   4   5
daegu    6   7   8
incheon  9  10  11
b    0
e    1
f    2
dtype: int64
           b   d     e   f
seoul    0.0 NaN   3.0 NaN
kwangju  3.0 NaN   6.0 NaN
daegu    6.0 NaN   9.0 NaN
incheon  9.0 NaN  12.0 NaN
실습7.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.arange(12).reshape(4,3), columns=list('bde'),
 index=['seoul', 'kwangju', 'daegu', 'incheon'])
print(df)
s3 = df['d']
print(s3)
print(df+s3)
[결과]
         b   d   e
seoul    0   1   2
kwangju  3   4   5
daegu    6   7   8
incheon  9  10  11
seoul       1
kwangju     4
daegu       7
incheon    10
Name: d, dtype: int32
          b   d  daegu   e  incheon  kwangju  seoul
seoul   NaN NaN    NaN NaN      NaN      NaN    NaN
kwangju NaN NaN    NaN NaN      NaN      NaN    NaN
daegu   NaN NaN    NaN NaN      NaN      NaN    NaN
incheon NaN NaN    NaN NaN      NaN      NaN    NaN
실습8.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.arange(12).reshape(4,3), columns=list('bde'),
 index=['seoul', 'kwangju', 'daegu', 'incheon'])
print(df)
s3 = df['d']
print(s3)
#행에 대한 연산을 수행할 경우에는 함수를 이용한다.
print(df.add(s3, axis=0))
print(df.sub(s3, axis=0))
[결과]
         b   d   e
seoul    0   1   2
kwangju  3   4   5
daegu    6   7   8
incheon  9  10  11
seoul       1
kwangju     4
daegu       7
incheon    10
Name: d, dtype: int32
          b   d   e
seoul     1   2   3
kwangju   7   8   9
daegu    13  14  15
incheon  19  20  21
         b  d  e
seoul   -1  0  1
kwangju -1  0  1
daegu   -1  0  1
incheon -1  0  1
댓글목록
등록된 댓글이 없습니다.
