1.寫入excel:
df = pd.DataFrame(resultlist)#一個(gè)列表遏匆,列表里裝的是json格式的數(shù)據(jù)
df.to_excel('data.xlsx', encoding='utf-8', startcol=0, index=False)
2.讀取excel
df = pd.read_excel('data.xlsx', encoding='utf-8', usecols='A,C:H,R:AB')
3.去掉重復(fù)的行
df.drop_duplicates(subset = [name],keep='first',inplace=True)
4.列名
df= pd.read_excel('a.xlsx', encoding='utf-8')
print(df.columns)
df.groupby(['id','b']):
6.遍歷
比較快的是
for tup in df.itertuples():
print(tup[0])#索引
print(tup[1])#和excel中的列一一對(duì)應(yīng)
print(tup[2])
7.條件查詢
df.loc[df["列名"] == "sth"]#返回符合條件的行
df.loc[df["列名"] == "sth","列名"] 單元格
這樣寫會(huì)返回類型,如果只要值
df.loc[df["列名"] == "sth","列名"] .values#返回的是列表
有多個(gè)條件的用&
df.loc[(df['A']=='a') & df['A']=='b') ]
8.問(wèn)題1:
Index(['Unnamed: 0', 'Unnamed: 1', 'Unnamed: 2', 'Unnamed: 3'], dtype='object')
查詢列名時(shí)報(bào)以上錯(cuò)誤
原因:默認(rèn)查詢excel的sheet1,numbers導(dǎo)出excel時(shí)會(huì)增加一個(gè)sheet1蝠引,刪掉即可
9.判斷單元格是否為null
if tup[i] is np.nan
10:將兩列合并成一列
parent_teacher_data['address'] = parent_teacher_data['country']+parent_teacher_data['province']+parent_teacher_data['city']+parent_teacher_data['county']
11:追加寫入
如果是用to_excel追加比較麻煩院领,所以改用csv,mode='a'
df.to_csv("candidaterule.csv", encoding='utf-8',mode="a",index=None)
我是將json寫入csv的杠园,如果直接這樣,每次追加寫入都有一行列名