과기정통부, 부처 협업 기반 공공분야 AI 활용 확산한다
이스트소프트, 이큐비알과 AI 휴먼 키오스크 사업 협력
AI 모델 학습비용 증가 해결한다…국가AI연구거점, 첫 성과 보고
[데스크가 만났습니다] 윤완수 웹케시그룹 부회장 “금융, 말로하는 시대”
EU, 머스크 'X' 디지털법 위반 들여다본다…xAI의 X 인수도 조사
dates = pd.date_range('2021-07-01','2021-07-06')
print(dates)
print(type(dates))
DatetimeIndex(['2021-07-01', '2021-07-02', '2021-07-03', '2021-07-04',
'2021-07-05', '2021-07-06'],
dtype='datetime64[ns]', freq='D')
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
temps1 = pd.Series([80,82,85,90,83,87], index =dates)
temps1
2021-07-01 80
2021-07-02 82
2021-07-03 85
2021-07-04 90
2021-07-05 83
2021-07-06 87
Freq: D, dtype: int64
[80,82,85,90,83,87] 부분이 이미지일 수도 있고, 결제 기록일 수도 있음
Freq: D는 해당 시계열 인덱스(날짜 인덱스)의 주기(frequency)가 '하루(Day)'임을 의미
- 사진 메타정보(EXIF)
- 사진과 관련된 추가적인 정보를 담고 있는 데이터
- 사진의 촬영 날짜, 시간, 카메라 모델, 위치 정보, 설정값 등 다양한 내용을 포함
- EXIF(Exchangeable Image File Format): 표준 규격
아래의 코드는 두 개의 시리즈를 이용하여 데이터 프레임을 만듭니다.
아래의 코드는 두 개의 컬럼을 갖는 Dataframe 객체 temps_df를 만든다
이 데이터 프레임의 구성요소인 컬럼(세로로 길죽한것)은 위에서 만든 temps1 과 temps2의 시리즈 객체이다.
위의 셀에서 언급한 것처럼 temps1과 temps2는 동일한 인덱스인 dates를 가지므로
데이터 프레임이라는 데이터 구조를 만들때 2021-07-01 부터 2021-07-06까지의 인덱스가
temps1과 temps2에서 공통적으로 사용되었기에 데이터 프레임의 인덱스가 되기에 어려움이 없다(유리하다).
temps_df = pd.DataFrame( { 'Missoula': temps1, 'Pliladelphia': temps2 } )
print(temps_df)
df['NewColumn'] = [1, 2, 3, 4]
insert(loc=, column=, value=)df.insert(loc=1, column='NewColumn', value=[1, 2, 3, 4])
df2 = pd.DataFrame({'NewColumn': [1, 2, 3, 4]})
df = pd.concat([df, df2], axis=1)
df = df.assign(NewColumn=[1, 2, 3, 4])
columns = 데이터프레임 이름.columns.tolist() + 추가하고 싶은 열 리스트 형태로 열을 추가할 수 있음df2 = df.reindex(columns = df.columns.tolist() + ['추가1', '추가2']
axis=df['추가'] = df.apply(lambda _: None, axis=1)
# 샘플 DataFrame 생성
import pandas as pd
data = {'Name': ['John', 'Alex', 'Peter'],
'Age': [25, 24, 28],
'Gender': ['Male', 'Male', 'Male']}
df = pd.DataFrame(data)
# 'Age' 열의 이름을 'Years'로 변경
df = df.rename(columns={'Age': 'Years'})
# DataFrame 출력
print(df)
Name Years Gender
0 John 25 Male
1 Alex 24 Male
2 Peter 28 Male
# 샘플 DataFrame 생성
import pandas as pd
data = {'Name': ['John', 'Alex', 'Peter'],
'Age': [25, 24, 28],
'Department': ['IT', 'HR', 'Marketing']}
df = pd.DataFrame(data)
# 'Age' 및 'Department' 열 이름 변경
df = df.rename(columns={'Age': 'Years', 'Department': 'Dept'})
# DataFrame 출력
print(df)
Name Years Dept
0 John 25 IT
1 Alex 24 HR
2 Peter 28 Marketing
# Create a sample DataFrame
import pandas as pd
data = {'Name': ['John', 'Alex', 'Peter'],
'Age': [25, 24, 28],
'Department': ['IT', 'HR', 'Marketing']}
df = pd.DataFrame(data)
# Rename the '2' to 'Dept' column by index
df = df.rename(columns={2: 'Dept'})
# Print the DataFrame
print(df)
Name Age Dept
0 John 25 IT
1 Alex 24 HR
2 Peter 28 Marketing
# Create a sample DataFrame
import pandas as pd
data = {'Name': ['John', 'Alex', 'Peter'],
'Age': [25, 24, 28],
'Department': ['IT', 'HR', 'Marketing']}
df = pd.DataFrame(data)
# Rename the 'Name' and 'Department' columns using a list of column names
df.columns = ['ID', 'Years', 'Dept']
# Print the DataFrame
print(df)
ID Years Dept
0 John 25 IT
1 Alex 24 HR
2 Peter 28 Marketing
# Create a sample DataFrame
import pandas as pd
data = {'Name': ['John', 'Alex', 'Peter'],
'Age': [25, 24, 28],
'Department': ['IT', 'HR', 'Marketing']}
df = pd.DataFrame(data)
# Rename the 'Age' and 'Department' columns using list comprehension
df.columns = [col.replace('_', ' ').title() for col in df.columns]
# Print the DataFrame
print(df)
Name Age Department
0 John 25 IT
1 Alex 24 HR
2 Peter 28 Marketing
# Create a sample DataFrame
import pandas as pd
data = {'Name': ['John', 'Alex', 'Peter'],
'Age': [25, 24, 28],
'Gender': ['Male', 'Male', 'Male']}
df = pd.DataFrame(data)
# Rename the 'Age' and 'Gender' columns by index
df.columns = df.columns.set_axis(['a', 'Years', 'b'], axis=1, inplace=False)
# Print the DataFrame
print(df)
Name Age Dept
0 John 25 IT
1 Alex 24 HR
2 Peter 28 Marketing
프로그래머가 코딩하는 입장에서 정리해 봅시다.

사고의 폭을 유연하게 가질 필요가 있음
: AX/DX 엔지니어가 되든, 서비스쪽이드, 모델러이든 상관 없이 모두 지녀야 하는 공통 분모
→ 외부 파일에 대한 이해가 필요
→ 운영체제, 데이터베이스, 서버 등에 대한 공부가 필요함
데이터 정제
최종 목표: 시각화 결과물

판다스의 시리즈는 각 인덱스 레이블 당 한 개의 값(values)만 가진다.
그러나 데이터 프레임을 사용하면 인덱스 한 개당 복수개의 값을 가질 수 있다
데이터 프레임은 인덱스 레이블을 기준으로 배열된 한 개 이상의 시리즈의 집합을 의미한다.
각 시리즈는 데이터 프레임의 컬럼(열)이며 각각의 컬럼(열)은 자신만의 이름을 갖는다.
데이터 프레임은 데이터 베이스의 테이블 개념과 비슷하다.
서로다른 유형의 데이터 컬럼을 한 개 이상 갖는다는 점과, 한 컬럼의 모든 데이터는 동일한 유형이라는 점이 비슷한 점이다.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range('2021-07-01','2021-07-06')
temps1 = pd.Series([80,82,85,90,83,87], index=dates)
temps2 = pd.Series([70,75,69,83,79,77], index=dates)
temps_df = pd.DataFrame({'Missoula':temps1, 'Philadelphia':temps2})
print(temps_df['Missoula']) # 데이터프레임에서 특정한 컬럼을 참조하기
print(type(temp_df)) # temps_df 는 데이터프레임 형식임
print('---')
print(type(temps1))
print(type(temps_df['Missoula']))
aa = temps_df['Missoula']
print(type(aa))
bb = temps_df.Missoula # 데이터프레임에 속해 있는 시리즈를 꺼내오는 새로운 문법임
# 직전의 temps_df['Missoula'] 문법과 비교할 것
print(bb)
print(type(bb))
2021-07-01 80
2021-07-02 82
2021-07-03 85
2021-07-04 90
2021-07-05 83
2021-07-06 87
Freq: D, Name: Missoula, dtype: int64
<class 'pandas.core.frame.DataFrame'>
---
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
2021-07-01 80
2021-07-02 82
2021-07-03 85
2021-07-04 90
2021-07-05 83
2021-07-06 87
Freq: D, Name: Missoula, dtype: int64
<class 'pandas.core.series.Series'>

# 두 가지 문법 비교
temps_df['Missoula']
temps_df.Missoula
temps_df['Missoula']
[]) 안에 컬럼 이름을 문자열로 넣어서 해당 컬럼(시리즈)을 선택temps_df['Missoula City'] 처럼 공백이 있어도 OKcol = 'Missoula'; temps_df[col]temps_df.Missoula
.) 연산자를 사용해서 컬럼 이름을 직접 적어서 시리즈를 선택Missoula는 알파벳만 있으니 사용 가능, 하지만 temps_df.Missoula City는 에러 발생 (공백 불가)sum, mean 등)와 겹치면 사용 불가| 문법 | 동적 컬럼명 지원 | 공백/특수문자 컬럼명 가능 | 예약어 컬럼명 가능 | 일반적 권장 여부 |
|---|---|---|---|---|
temps_df['Missoula'] | O | O | O | O |
temps_df.Missoula | X | X | X | △ |
temps_df['Missoula']를 쓰세요.temps_df.Missoula도 사용 가능합니다.temps_df [[ 'Philadelphia','Missoula' ]]
Missoula Philadelphia
2021-07-01 80 70
2021-07-02 82 75
2021-07-03 85 69
2021-07-04 90 83
2021-07-05 83 79
2021-07-06 87 77
Freq: D, dtype: int64
temps_diff = temps_df.Missoula - temps_df.Philadelphia
print(temps_diff)
print(type(temps_diff))
print(temps_df)
2021-07-01 10
2021-07-02 7
2021-07-03 16
2021-07-04 7
2021-07-05 4
2021-07-06 10
Freq: D, dtype: int64
<class 'pandas.core.series.Series'>
Missoula Philadelphia
2021-07-01 80 70
2021-07-02 82 75
2021-07-03 85 69
2021-07-04 90 83
2021-07-05 83 79
2021-07-06 87 77
temps_df['Difference'] = temps_diff
print(temps_df)
Missoula Philadelphia Difference
2021-07-01 80 70 10
2021-07-02 82 75 7
2021-07-03 85 69 16
2021-07-04 90 83 7
2021-07-05 83 79 4
2021-07-06 87 77 10
print( temps_df.columns )
print( list(temps_df.columns) )
Index(['Missoula', 'Philadelphia', 'Difference'], dtype='object')
['Missoula', 'Philadelphia', 'Difference']
col_list = list(temps_df.columns)
col_list[0] = 'Misu'
temps_df.columns = col_list # 데이터프레임의 컬럼명(시리즈 레이블명)변경됨
print(list(temps_df.columns))
['Misu', 'Philadelphia', 'Difference']
print(temps_df)
Misu Philadelphia Difference
2021-07-01 80 70 10
2021-07-02 82 75 7
2021-07-03 85 69 16
2021-07-04 90 83 7
2021-07-05 83 79 4
2021-07-06 87 77 10
print(temps_df.Misu)
2021-07-01 80
2021-07-02 82
2021-07-03 85
2021-07-04 90
2021-07-05 83
2021-07-06 87
Freq: D, Name: Misu, dtype: int64
print(temps_df.Misu[1:4])
print(temps_df.Misu[3:])
2021-07-02 82
2021-07-03 85
2021-07-04 90
Freq: D, Name: Misu, dtype: int64
2021-07-04 90
2021-07-05 83
2021-07-06 87
Freq: D, Name: Misu, dtype: int64

[1:4]의 숫자는 내재된 인덱스(시스템 인덱스)임.loc는 인덱스 레이블을 사용해 검색.iloc는 제로베이스 위치를 사용해 검색print(temps_df)
print('-'*40)
print(temps_df.iloc[5])
temps_df.iloc[5][n], n번째 행을 참조하라는 뜻print(temps_df.iloc[3].index)
print(temps_df.loc['2021-07-06'])
Index(['Misu', 'Philadelphia', 'Difference'], dtype='object')
Misu 87
Philadelphia 77
Difference 10
Name: 2021-07-06 00:00:00, dtype: int64
print(temps_df.iloc[5])
Misu 87
Philadelphia 77
Difference 10
Name: 2021-07-06 00:00:00, dtype: int64
print('-'*40) print(temps_df.Seoul[-3:]) print('-'*40) print(temps_df.Seoul[3:6]) print('-'*40) print(temps_df[['Seoul']][3:]) print('-'*40) print(temps_df2['Seoul'].tail(3)) print('-'*40) print(temps_df.loc['20210704':'20210706',['Seoul']]) print('-'*40) print(temps_df.loc['2021-07-04':'2021-07-06',['Seoul']]) # 위의 소스와 비교---------------------------------------- 2021-07-04 82 2021-07-05 77 2021-07-06 85 Freq: D, Name: Seoul, dtype: int64 ---------------------------------------- 2021-07-04 82 2021-07-05 77 2021-07-06 85 Freq: D, Name: Seoul, dtype: int64 ---------------------------------------- Seoul 2021-07-04 82 2021-07-05 77 2021-07-06 85 ---------------------------------------- 2021-07-04 82 2021-07-05 77 2021-07-06 85 Freq: D, Name: Seoul, dtype: int64 ---------------------------------------- Seoul 2021-07-04 82 2021-07-05 77 2021-07-06 85 ---------------------------------------- Seoul 2021-07-04 82 2021-07-05 77 2021-07-06 85
temps_df.iloc[[ 1,3,5 ]: temps_df 데이터프레임에서 1,3,5번 인덱스 번호를 가져옴temps_df.iloc[[1, 3, 5]].Diff: temps_df 데이터프레임에서 가져온 1,3,5번 인덱스 번호 중 Column Diff(Series Diff) 에 해당하는 것만 추출%pfile 명령어
%pfile 모듈명.함수명 또는 %pfile 함수명처럼 사용하여 해당 객체가 정의된 파일의 내용을 볼 수 있음%pfile 명령어는 파이썬 객체(함수, 클래스, 모듈 등)의 소스 코드를 찾아주는 용도로 설계되어 있어 만약 사용자가 %pfile data/goog.csv와 같이 입력하면, 이 명령은 data/goog.csv라는 파이썬 객체(함수, 클래스, 모듈 등)가 있는지 찾으려고 시도합니다. goog.csv는 파이썬 객체가 아니라 데이터 파일(CSV 파일)이기 때문에, 파이썬 인터프리터는 해당 객체를 찾지 못하고 "Object data/goog.csv not found."라는 오류 메시지를 출력하게 됩니다.명령어가 하는 일: %pfile은 파이썬 객체의 소스 코드를 파일로 출력하는 IPython 매직 명령어입니다.
%pfile data/goog.csv 입력 시 에러 메시지가 나오는 이유: data/goog.csv는 파이썬 객체가 아니라 데이터 파일이므로, 해당 객체를 찾을 수 없어서 에러가 발생합니다.
pandas.read_csv("data/goog.csv")와 같은 명령어를 사용해야 합니다.