# -*- coding: utf-8 -*-
# pandastest1.py

import pandas as pd

mydf1 = pd.read_csv(r'D:\HWP\강의자료\파이썬\codes\bmi.txt', 
    names=['ht', 'wt', 'year', 'religion', 'gender', 'marriage'], 
                    header=None, sep='\s+')
print('Read from local: ', mydf1.shape)

url = "http://jupiter.hallym.ac.kr/ftpdata/data/bmi.txt"
mydf1 = pd.read_csv(url, names=['ht', 'wt', 'year', 'religion', 'gender', 'marriage'], 
                    header=None, sep='\s+')
print('Read from web: ', mydf1.shape)
print(mydf1.head())
print(mydf1.tail())
print(mydf1.describe())

mydf1.to_csv(r'D:\HWP\강의자료\파이썬\codes\bmi.csv', index=False)
#===========================================================================
'''
from tkinter import filedialog, Tk

# encoding이 euc-kr인 score-euc.csv 를 읽을 예정
root = Tk()
#mydf2 = pd.read_csv(filedialog.askopenfilename(parent=root), header=0, encoding='cp949')
mydf2 = pd.read_csv(filedialog.askopenfilename(parent=root), header=0, encoding='euc-kr')
root.withdraw()

print("opened by file dialog: ", mydf2.shape)
print(mydf2.head())
print(mydf2.tail())
print(mydf2.describe())
'''
#=========================================================================
mydf3 = pd.read_excel(r'D:\HWP\강의자료\파이썬\codes\score.xlsx', sheet_name='score')
print('Read from local excel file: ', mydf3.shape)

mydf1.to_excel(r'D:\HWP\강의자료\파이썬\codes\bmi.xlsx', sheet_name='bmi', index=False)

#========================================================================

mydfG = mydf1.groupby('gender')
print(mydfG.get_group('M'))

#=======================================================================

mydfM = mydf1[ mydf1['gender']=='M']  
print(mydfM)
print(mydf1[ mydf1['ht'] > 175] )

print(mydf1.describe())
print(mydf1.corr())

#======================================================================
qidx = mydf1.wt / (mydf1.ht/100) ** 2
mydf1['bmi'] = qidx
print(mydf1.head())

del mydf1['bmi']
print(mydf1.head())

mydf1.insert(2, 'bmi', qidx)
print(mydf1.head())
#=======================================================================




