from pandas import Series, DataFrame
import pandas as pd
from __future__ import division
from numpy.random import randn
import numpy as np
import os
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
import pandas as pd
obj = Series([4, 7, -5, 3])
0 4
1 7
2 -5
3 3
obj.values
array([ 4, 7, -5, 3], dtype=int64)
obj.index
RangeIndex(start=0, stop=4, step=1)
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan'] #index 이름 변경
Bob 4
Steve 7
Jeff -5
Ryan 3
obj2 = Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
d 4
b 7
a -5
c 3
obj = Series(range(3), index=['a', 'b', 'c'])
print(obj)
a 0
b 1
c 2
obj2['a']
obj2[['c', 'a', 'd']]
-5
c 3
a -5
d 4
obj2['d'] = 6
obj[[1, 3]]
b 1.0
d 3.0
값이 0보다 큰 것만 출력
obj2[obj2 > 0]
a 2.0
b 1.0
값에 * 2 해서 출력 (실제 series 내용이 바뀌진 않음.)
obj2 * 2
e의 값제곱
np.exp(obj2)
해당 index가 있는지
'b' in obj2 #True or False 반환
sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
obj3 = Series(sdata)
obj3
Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
states = ['California', 'Ohio', 'Oregon', 'Texas']
obj4 = Series(sdata, index=states) #index에 명시돼있는 것만 만들어짐.
obj4
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
labels = pd.Index(np.arange(3))
obj2 = pd.Series([1.5, -2.5, 0], index=labels)
obj2
0 1.5
1 -2.5
2 0.0
pd.isnull(obj4) #NaN인 거만 True
#obj4.isnull() #동일
California True
Ohio False
Oregon False
Texas False
pd.notnull(obj4) #NaN이 아니면 True
California False
Ohio True
Oregon True
Texas True
obj3 + obj4
obj4.name = 'population'
obj4.index.name = 'state'
obj4
state
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
Name: population, dtype: float64
obj4.index #각 index 이름이랑 index 전체 name 확인
Index(['California', 'Ohio', 'Oregon', 'Texas'], dtype='object', name='state')
obj = Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c']) #를
obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e']) #로 변경. (e는 NaN)
a -5.3
b 7.2
c 3.6
d 4.5
e NaN
obj.reindex(['a', 'b', 'c', 'd', 'e'], fill_value=0)
a -5.3
b 7.2
c 3.6
d 4.5
e 0.0
obj3 = Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])
obj3.reindex(range(6), method='ffill')
0 blue
2 purple
4 yellow
↓
0 blue
1 blue
2 purple
3 purple
4 yellow
5 yellow
obj = Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
new_obj = obj.drop('c')
obj.drop(['d', 'c']) # 하나 이상의 열 삭제
obj.drop('c', inplace=True) #'c' index 삭제
obj[1] #index가 1번째인 값 출력 -> ex) 1.0
obj[2:4] #index가 2번째부터 3번째인 index와 값 출력
c 2.0
d 3.0
obj['b':'c']
b 1.0
c 2.0
obj['b':'c'] = 5
a 0.0
b 5.0
c 5.0
d 3.0
obj = Series(range(4), index=['d', 'a', 'b', 'c'])
print(obj)
obj.sort_index() #index를 a. b. c. d로 정렬
obj = Series([4, 7, -3, 2, np.nan])
obj.sort_values() #-3, 2, 4, 7, NaN로 값이 정렬
obj = Series([7, -5, 7, 4, 2, 0, 4])
obj.rank()
# 동일 데이터는 관찰된 순서대로 순위 부여
obj.rank(method='first')
# 내림차순으로 순위를 매김: ascending=False 옵션
# method='max': 동일 데이터는 순위가 max로
obj.rank(ascending=False, method='max')
obj.index.is_unique #중복된 게 있으면 False return
obj = Series(['a', 'a', 'b', 'c'] * 4)
obj.describe()