Pandas 데이터분석 기초 실습 -2
2021. 9. 22. 17:15ㆍ빅데이터 스터디
In [1]:
import pandas as pd
In [6]:
friend_dict_list = [
{'name':'John','age':25,'job':'student'},
{'name':'Nate','age':30,'job':'teacher'}
]
df = pd.DataFrame(friend_dict_list)
df.head()
#딕셔너리의 경우 key 값의 순서가 보장되지 않는다
Out[6]:
name | age | job | |
---|---|---|---|
0 | John | 25 | student |
1 | Nate | 30 | teacher |
In [8]:
df = df[['name','age','job']] #순서지정을 다시 해줘야 한다
df.head()
Out[8]:
name | age | job | |
---|---|---|---|
0 | John | 25 | student |
1 | Nate | 30 | teacher |
ordered dictinonary 를 이용해서 한번에 dataframe 만들기¶
In [9]:
from collections import OrderedDict
In [13]:
friend_ordered_dict = OrderedDict(
[
('name',['John','Nate']),
('age',[25,30]),
('job',['student','teacher'])
]
)
df = pd.DataFrame.from_dict(friend_ordered_dict)
df.head()
Out[13]:
name | age | job | |
---|---|---|---|
0 | John | 25 | student |
1 | Nate | 30 | teacher |
list를 사용해서 dataframe 만들기¶
In [14]:
friend_list =[
['John',20,'student'],
['Nate',30,'teacher']
]
In [15]:
column_name =['name','age','job']
In [17]:
df = pd.DataFrame.from_records(friend_list,columns= column_name)
df.head()
Out[17]:
name | age | job | |
---|---|---|---|
0 | John | 20 | student |
1 | Nate | 30 | teacher |
In [35]:
friend_list =[
['name',['John','Nate']],
['age',[20,30]],
['job',['student','teacher']]
]
#df = pd.DataFrame.from_items(friend_list)
#items 함수 삭제됨 다른 방법택할것
'''Deprecated since version 0.23.0: from_items is deprecated and will be removed in a future version.
Use DataFrame.from_dict(dict(items)) instead.
DataFrame.from_dict(OrderedDict(items))
may be used to preserve the key order.'''
df = pd.DataFrame.from_dict(dict(friend_list)) #1
df = pd.DataFrame.from_dict(OrderedDict(friend_list))#2 둘중에 하나 택 해서 해결
df.head()
Out[35]:
name | age | job | |
---|---|---|---|
0 | John | 20 | student |
1 | Nate | 30 | teacher |
In [33]:
print(dict(friend_list)) #1
{'name': ['John', 'Nate'], 'age': [20, 30], 'job': ['student', 'teacher']}
In [34]:
print(OrderedDict(friend_list)) #2
OrderedDict([('name', ['John', 'Nate']), ('age', [20, 30]), ('job', ['student', 'teacher'])])
'빅데이터 스터디' 카테고리의 다른 글
Pandas 데이터분석 기초 실습 -4 (0) | 2021.09.22 |
---|---|
Pandas 데이터분석 기초실습 -3 (0) | 2021.09.22 |
Pandas 데이터 분석 기초 실습-1 (0) | 2021.09.22 |
30분 요약 강좌 시즌2 : Python 활용편 섹션6-폴리움 (0) | 2021.09.17 |
30분 요약 강좌 시즌2 : Python 활용편-섹션4-웹크롤링 연습문제2 (0) | 2021.09.17 |