상세 컨텐츠

본문 제목

pandas 기능 정리(1)

Python

by Sam_Park 2021. 12. 30. 23:52

본문

import pandas as pd

series1 = pd.Series([1,2,3,4,5])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 1. series 연산,  추가,  삭제
population /1000000                # series 연산 : value 에만 영향을 줌 (사칙연산 가능)
population - population2        # 시리즈 끼리의 연산 
 
 
# 2. series인덱싱 
population[1]
population['부산']                         # 인덱스, '이름' 둘 다 가능
population['부산':'대구']                #'부산':'대구'는 '대구' 포함결과
 
 
#  []  리스트 안에 넣음으로써 다중 인덱싱
population[ ['인천''서울','인천'] ]                        #또는 [ [0,3,1] ]              
population[(population<=5000000& (population>=2500000)]    #boolean 인덱싱 & 연산자 사용법
ds = population - population2  
ds[ds.notnull()]                                        #또는 ds[ds.isnull()]  # null 여부로 인덱싱  
 
 
 
# 3.  데이터 프레임 생성과 전치
data = [ 
    [99043129631482],
    [34487273393191],
    [28904516232035],
    [24660522431774]
]
ind = ['서울','부산','인천','대구']      # 직접 입력 가능, 별도 선언 해서 입력도 가능 
col = ['2015','2010']        
 
df3 = pd.DataFrame(data,  index = ind ,  columns = col)       # 별도 선언한 index ,col 이름 대입
print(df3)
 
df2 = df3.T        # 전치 기능 df.T  ,  df.transpose() 이것도 천치, transpose()는 numpy에서도 가능
 
 
#  4. 데이터프레임 정보 확인 
df.index = ['서울''부산','인천','대구']        # 데이터 프레임을 만든 후에 인덱스 설정
# 값 , 인덱스 , 컬림  : values , index , columns
print(df2.values)
print(df2.index)
print(df2.columns)            
 
 
# 5. 데이터프레임 고급 인덱싱: 인덱서, iloc: 인덱스 번호 / loc: 지정한 인덱스   [index , columns]
# ('2005'==컬럼 이름) 각 컬럼이 하나의 시리즈라고 보면 됨. 기본이 컬림 기준
 
df2['2005'= [9762546,3512547,2517680,'dd']        # 2005 컬럼 value 를 Series 로 출력
df2[['2015']]                                        # 2015 컬럼 value 를 DataFrame 으로 출력
 
# loc
df2.loc['서울':'부산''2015':'2010']            # 주로 loc 를 씀
 
df2.loc[df2.loc[:,'2010']>=2500000]                # 불리언 인덱싱 가능
 
bol = df2.loc[:,'2010']>=2500000
df2.loc[bol,:]                                # bol행열 구분한 버전, [인덱스, 컬럼 ] 구분해야 함.
 
 
 
 
 
 
 
cs

 

관련글 더보기

댓글 영역