잠토의 잠망경

[Pandas] Data 만들기(Series, DataFrame) 본문

공부/Python

[Pandas] Data 만들기(Series, DataFrame)

잠수함토끼 2018. 12. 23. 15:33

Series 만들기

가장 단순한 방식, index 지정 안함

import pandas as pd

s = pd.Series(['a',42])

print(s)
print(type(s))

output

>>> print(s)
0     a
1    42
dtype: object
>>> print(type(s))
<class 'pandas.core.series.Series'>

Series 만들기, index를 지정

import pandas as pd
s = pd.Series(['a',42],index=['name','age'])

print(s)
print(type(s))

output

>>> print(s)
name     a
age     42
dtype: object
>>> print(type(s))
<class 'pandas.core.series.Series'>

DataFrame 만들기

import pandas as pd

s = pd.DataFrame({'Name':['a','b'],
                    'Address':['Seoul','Asan'],
                    'occupation':['doctor','IT'],
                    'Born':['1981-01-10','1999-11-02'],
                    'Died':['2100-01-10','2099-01-02'],
                    'Age':[40,20]})

print(s)
print(type(s))

output

>>> print(s)
  Address  Age        Born        Died Name occupation
0   Seoul   40  1981-01-10  2100-01-10    a     doctor
1    Asan   20  1999-11-02  2099-01-02    b         IT
>>>
>>> print(type(s))
<class 'pandas.core.frame.DataFrame'>

DataFrame 만들기, index를 지정

columns로 원하는 column의 위치와 숨김 표현을 구현할 수 있다.

index를 통하여 dataFrame의 index로 넣을 수 있다.

import pandas as pd

s = pd.DataFrame({  'Address':['Seoul','Asan'],
                    'occupation':['doctor','IT'],
                    'Born':['1981-01-10','1999-11-02'],
                    'Died':['2100-01-10','2099-01-02'],
                    'Age':[40,20]},
                    index=['a','b'],
                    columns=['occupation', 'Born', 'Died','Age'])

print(s)

output

>>> print(s)
  occupation        Born        Died  Age
a     doctor  1981-01-10  2100-01-10   40
b         IT  1999-11-02  2099-01-02   20

순서보장 Dictionary를 활용

데이터 입력순서를 그대로 만들어 준다.

import pandas as pd

from collections import OrderedDict

s = pd.DataFrame(
    OrderedDict([
        ('Name',['a','b']),
        ('Address',['Seoul','Asan']),
        ('occupation',['doctor','IT']),
        ('Born',['1981-01-10','1999-11-02']),
        ('Died',['2100-01-10','2099-01-02']),
        ('Age',['40','20']),
    ])
)
print(s)

output

>>> print(s)
  Name Address occupation        Born        Died Age
0    a   Seoul     doctor  1981-01-10  2100-01-10  40
1    b    Asan         IT  1999-11-02  2099-01-02  20
Comments