pandas_to_excel()

pandas document 공식문서

pandas

# import pandas as pd

pd.Series([1,2,3,4,5]) # 시리즈 타입으로 변환
"""
0    1
1    2
2    3
3    4
4    5
dtype: int64
"""

pandas_딕셔너리

## 임의의 데이터프레임을 생성할 때는 딕셔너리를 추천

temp = pd.DataFrame({
    'A' : [1,2,3],
    'B' : ['A', 'B', 'C']
})
"""
	A	B
0	1	A
1	2	B
2	3	C
"""

pandas_rename

pd_l=[1, 2, 3, 4, 5]
pd_s2 = pd.DataFrame(pd_1) # 데이터프레임으로 저장
pd_s2['1열'] = pd_1 # 열 이름 변경
pd_s2
"""
	0	1열
0	1	1
1	2	2
2	3	3
3	4	4
4	5	5
"""

# 데이터 프레임의 0번째 열은 0이기 때문에 rename (이름 변경)이 필요함
pd_s3=pd_s2.rename(columns={0:'0열'})
pd_s3
"""
	0열	1열
0	1	1
1	2	2
2	3	3
3	4	4
4	5	5
"""

score_df_r=score_df.rename(columns={'국어':'korean', '영어':'english', '수학':'math'})
score_df_r
"""
성명	korean	english	math
0	정약용	70	80	100
1	이순신	100	80	70
2	이율곡	90	95	90
"""

object type

n=pd.DataFrame({'col_str1':['1', '2', '3', '4'],
 'col_str2':['5', '6', '7', '8']})
 
n.info()
"""
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   col_str1  4 non-null      object
 1   col_str2  4 non-null      object
dtypes: object(2)
memory usage: 196.0+ bytes
"""

# object type은 형식이 정의가 안된 것을 의미.
# 따라서 형변환을 시켜 일치시켜줘야함.
n_df=n.astype({'col_str1':int, 'col_str2':float}) # 형변환
print(n_df)
n_df.dtypes
"""
   col_str1  col_str2
0         1       5.0
1         2       6.0
2         3       7.0
3         4       8.0
col_str1      int64
col_str2    float64
dtype: object
"""

dataframe_index, set_index

특정 열을 인덱스로 지정하기