Excel,  Python

[Python] CSV/XLS 파일 활용

FavoriteLoadingAdd to favorites

================================================
Sample – 1 csv 파일 읽고 표시
pip install xlrd
pip install openpyxl
================================================
import pandas as pd
folder1 = ‘/data/’
xls_file1 = folder1 + ‘usa1.xlsx’
csv_file1 = folder1 + ‘usa1.csv’
df = pd.read_excel(xls_file1)
df.to_csv(csv_file1, encoding=”cp949″, index=False)
print(“output csv:”, csv_file1)
================================================

import pandas as pd

csv_file1 = “/usa1.csv”
df = pd.read_csv(csv_file1, encoding=”utf8″, index_col=”연도”)
df.mean()
df.describe()
================================================
pip install XlsxWriter

import xlsxwriter

folder1 = ‘/data/’
xls_file1 = folder1 + ‘xlstest1.xlsx’

workbook = xlsxwriter.Workbook(xls_file1)
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 100)

workbook.close()

print(“created”, xls_file1)
================================================
여러개의 엑셀 파일 합치기 Merged
================================================
import pandas as pd
from pathlib import Path

input_folder = ‘/input’
raw_data_dir = Path(input_folder)
excel_files1 = raw_data_dir.glob(‘*’)

total_df1 = pd.DataFrame()

for excel in excel_files1:
df = pd.read_excel(excel_files1)
total_df1 = total_df1.append(df, ignore_index= True)

print(total_df1)

folder2 = ‘/input/’

merged_xls_file1 = folder2 + ‘totalsales.xlsx’

total_df.to_excel(merged_xls_file1, sheet_name=’totalmerged’, index=False)

print(“created”, merged_xls_file1)
===============================================
중간/기말, + 평균 구하기
===============================================
import pandas as pd

folder1 = ‘/data/’

xls_file1 = folder1 + “score.xlsx”

df1 = pd.read_excel(xls_file1)
print(df1)

df1_mean = df1[[‘mid’,’last’]].mean(axis=1)

df1[‘avg’] = df1_mean

print(df1)

error: Content is protected !!