"""
Created on Mon Mar 18 13:22:42 2019
@author: 1277
"""
# -*- coding: utf-8 -*-
import pandas as pd
f='d:\\pyTest\\test.csv'
f = pd.read_csv(f)
print('======前三筆')
print(f.head(3))
print('======後三筆')
print(f.tail(3))
print('======shape')
print(f.shape)
print('======info')
print(f.info())
print('======查詢 DataFrame 0至3筆 ======')
print(f[['Subject','Status']][0:3])
print('======新增 DataFrame columns 方法一')
f.insert(3,column="sport",value="Basketball")
print(f[['Subject','sport','Result']][0:])
print('======新增 DataFrame columns 方法二')
#Dictinary轉DataFrame使用
data = {'name': ['Bob', 'Nancy','Amy','Elsa','Jack'],
'year': [1996, 1997, 1997, 1996, 1997],
'month': [8, 8, 7, 1, 12],
'day':[11,23,8,3,11]}
myframe3 = pd.DataFrame(data,columns=['name','year', 'month', 'day','luckynumber'])
#先List轉成Series
luckynumber = ['3','2','1','7','8']
luckynumber = pd.Series(luckynumber)
#賦予值
myframe3['luckynumber'] = luckynumber
print(myframe3)
print('======新增 DataFrame 一列')
#Dictinary轉成Series
dic_data = {'name':'AAA','year':1955,'month':2,'day':2,'luckynumber':7 }
my_obj = pd.Series(dic_data)
#加入一列
newdata = myframe3.append(my_obj,ignore_index=True)
print(newdata)
# https://www.jianshu.com/p/7df2593a01ce
#也可用concat方式
insert=[['BBB',1999,12,12,55],['CCC',1911,11,11,66]]
insertRow = pd.DataFrame(insert,columns=['name','year', 'month', 'day','luckynumber'])
above = newdata.loc[:2]
below = newdata.loc[3:]
newdata2=pd.concat([above,insertRow,below],ignore_index=True)
print(newdata2)
print('======資料刪除 有N/A 或空白 的列')
f_delna = f.dropna()
print(f_delna[['Subject','sport','Result']][0:])
print('======空白資料填充')
f_na=f.fillna('xxxxx')
print(f_na)
print('======改值方式')
#.loc是對原有DataFrame的一種復制性引用,而[]的引用則是指針性的引用。
#所以用[]賦予值時出現SettingWithCopyWarning是正常的
A = pd.DataFrame([[1,2,3],[2,3,4],[3,4,5]], columns = ['a','b','c'])
B = A[['a', 'b']]
print(B)
B['a'] = B['a'] + 1 # same result by using B.loc[:,'a'] = B.loc[:,'a']+ 1
print(B)
#用.loc這樣就不會出現報警了。
C = A.loc[:,['a','b']]
C['a'] = C['a']+1