Python - Pandas

박준영·2020년 3월 19일
0

Python

목록 보기
1/3

Data Structure

Series (1D)

  • 시리즈 데이터는 1차원 배열형태라 생각하면된다.

Create Series

import pandas as pd
# 인덱스명 a, b를 갖고 데이터 1, 2를 갖는 시리즈 데이터를 만드시오.
series = pd.Series([1,2], index = ['a', 'b'])
----------------------------------------------
a    1
b    2

Get Series Value

import pandas as pd
# numpy 데이터 보기 
series.values
-------------------
array([1, 2])

Get Values by Index

import pandas as pd
#  인덱스를 2개 이상불러오려면 대괄호가 2개필요하다.
series['a']
series[['b', 'a']]
---------------------
1
b    2
a    1

DataFrame (2D)

  • 행과 열이 임의로 정렬되어있는 표 데이터 구조로 행렬구조를 갖고있다.

Create DF

import pandas as pd

dict1 = {'country': ['CH', 'KR'], 'year': [2000,2010]}
# dict1을 DataFrame형식으로 만들 수 있다. 
df = pd.DataFrame(dict1)
-------------------------------------------------------
	country	year
0	  CH	2000
1	  KR	2010
-------------------------------------------------------
# df 데이터프레임에 인덱스명을 지정해준다.
df1 = pd.DataFrame(df, index=["row_1","row_2"])
-------------------------------------------------------
	country	year
row1	  CH	2000
row2	  KR	2010

Get Columns and Row Names

import pandas as pd
# 데이터프레임의 인덱스명과 컬럼명 불러오기
df1.index
df1.columns
----------------------------------
Index(['row1', 'row2'], dtype='object')
Index(['country', 'year'], dtype='object')

Get Values

import pandas as pd
# numpy 형태의 데이터보기(array)
df1.values
-----------------------------
array([['CH', 2000],
       ['KR', 2010], dtype=object)

Reshaping

import pandas as pd

df = pd.DataFrame(
	{"a" : [4, 5, 6],
	"b" : [7, 8, 9],
	"c" : [10, 11, 12]},
	index = [1, 2, 3])
--------------------------
	a	b	c
1	4	7	10
2	5	8	11
3	6	9	12
--------------------------
# c 컬럼 drop하기
df.drop(['c'], axis=1)
--------------------------
	a	b
1	4	7
2	5	8
3	6	9

Group Data

import pandas as pd

0개의 댓글