Python自動(dòng)化辦公--Pandas玩轉(zhuǎn)Excel(全30集)

前兩節(jié):
import pandas as pd file = r'D:\desktop\K-RPA\work\數(shù)字廣東\Excel\巡檢結(jié)果Excel\專區(qū)內(nèi)鏈\used\主題集成20220623163524.xls' print(file) # 創(chuàng)建Excel文件并寫入 # pf = pd.DataFrame({'id': [1,2,3], 'Name': ['dsf','sdf','ret']}) # pf = pf.set_index('id') # pf.to_excel(file) # # print(pf) ''' 讀取Excel header:指定表頭,參數(shù)=None:不設(shè)表頭 index_col:指定列索引 ''' data = pd.read_excel(file,header=0, index_col=0) # print(f"行列數(shù):{data.shape}") # print(f"列:{data.columns}") # print(f"默認(rèn)前五行:{data.head()}") # print(f"默認(rèn)后五行:{data.tail()}") # 設(shè)置表頭 # data.columns = ('id','name','erj','sd','fg','tyr','ret','ds') # 指定行索引 # data.set_index('id', inplace=True) # data.to_excel(r'D:\desktop\K-RPA\work\數(shù)字廣東\Excel\巡檢結(jié)果Excel\專區(qū)內(nèi)鏈\used\test.xls') # print(data)
?
課時(shí)03.如何在pandas里自由如風(fēng)... P3 - 01:22
?# 索引相同時(shí) # s = pd.Series([1, 2, 3], index=[1, 2, 3], name='A') # s2 = pd.Series([10, 20, 30], index=[1, 2, 3], name='B') # s3 = pd.Series([100, 200, 300], index=[1, 2, 3], name='C') # # df = pd.DataFrame({s.name: s, s2.name: s2, s3.name: s3}) # print(df) # df = pd.DataFrame([s, s2, s3]) # print(df) # 索引不同時(shí) # s = pd.Series([1, 2, 3], index=[1, 2, 3], name='A') # s2 = pd.Series([10, 20, 30], index=[1, 2, 3], name='B') # s3 = pd.Series([100, 200, 300], index=[2, 3, 4], name='C') # # df = pd.DataFrame({s.name: s, s2.name: s2, s3.name: s3}) # print(df) # df = pd.DataFrame([s, s2, s3]) # print(df)
?
課時(shí)04.好用到爆的自動(dòng)填充功能... P4 - 00:13
?''' Excel填充 dtype:類型轉(zhuǎn)換 ''' file = r'D:\desktop\K-RPA\work\數(shù)字廣東\Excel\巡檢結(jié)果Excel\專區(qū)內(nèi)鏈\used\python s.xlsx' print(file) books = pd.read_excel(file, skiprows=6, usecols='C:G', index_col=None, dtype={"序號(hào)": str, 'title': str, 'time': str}) start = date(2022,6,28) for i in books.index: # 序號(hào)填充 books["序號(hào)"].at[i] = i + 1 # title填充 books["title"].at[i] = '偶數(shù)填充' if i % 2 == 0 else '奇數(shù)填充' # time填充 books["time"].at[i] = start print(books)
?
課時(shí)05.有點(diǎn)小費(fèi)周折的年、月、... P5 - 00:15
?''' Excel填充 dtype:類型轉(zhuǎn)換 ''' # 月份加1 def add_month(d, md): yd = md // 12 m = d.month + md % 12 if m != 12: yd += m // 12 m = m % 12 return date(d.year + yd, m, d.day) file = r'D:\desktop\K-RPA\work\數(shù)字廣東\Excel\巡檢結(jié)果Excel\專區(qū)內(nèi)鏈\used\python s.xlsx' print(file) books = pd.read_excel(file, skiprows=6, usecols='C:G', index_col=None, dtype={"序號(hào)": str, 'title': str, 'time': str}) start = date(2022,6,28) for i in books.index: # 序號(hào)填充 # books["序號(hào)"].at[i] = i + 1 books.at[i, "序號(hào)"] = i + 1 # title填充 # books["title"].at[i] = '偶數(shù)填充' if i % 2 == 0 else '奇數(shù)填充' books.at[i, "title"] = '偶數(shù)填充' if i % 2 == 0 else '奇數(shù)填充' # time填充:天數(shù)加1 # books["time"].at[i] = start + timedelta(days=i) # 年日加1 # books["time"].at[i] = date(start.year + i, start, start.day + 1) # 月份加1 # books["time"].at[i] = add_month(start,i) books.at[i, "time"] = add_month(start, i) print(books) # 寫入Excel # books.set_index("序號(hào)", inplace=True) # books.to_excel(file)
標(biāo)簽: