編程是使用代碼實現(xiàn)想法的過程径缅,同一個想法可能反復(fù)用到的是同一部分的代碼掺栅,現(xiàn)將這些代碼保存在推送里箱吕,以便用到的時候備查。以下語言均為Python柿冲。
在cmd中輸入如下代碼茬高,合并多個jupyter notebook文件。
nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged.ipynb
以下代碼用到的庫假抄。
import xlrd
import re
import sys
import pandas as pd
正則匹配怎栽。
re.match(r'Publications*', f) #返回True或者False
將打印在控制臺內(nèi)容導(dǎo)出到txt。
outputfile = open("./結(jié)果.txt","a")
sys.stdout = outputfile
使用xlrd獲得數(shù)據(jù)(通常用來處理不規(guī)則的Excel表)宿饱,關(guān)于xlrd熏瞄,詳細(xì)參考:https://www.cnblogs.com/insane-Mr-Li/p/9092619.html。
filePath = 'my_sheets.xls'
data = xlrd.open_workbook(filePath)
table = data.sheets()[0]
?
rows = table.row_values(col_index, start_rowx=0, end_rowx=None) #獲取特定行數(shù)據(jù)
cols = table.col_values(col_index, start_rowx=0, end_rowx=None) #取特定列數(shù)據(jù)
this_name = table.cell_value(0, 1) #獲取某個單元格數(shù)據(jù)
去除有空值的行谬以。
df = df.dropna(axis=0,subset=['source','type'],how='any')
將時間的字符串形式轉(zhuǎn)化為時間戳格式强饮。
time_df['date'] = pd.to_datetime(time_df['date'])
排序函數(shù)。
time_df.sort_values(by=['date'], ascending=True, inplace=True
讀取時間戳的月份和季度为黎。在http://www.reibang.com/p/0b8dcbc2df33一文中對時間戳處理的各種函數(shù)邮丰。
time_df['period'] = time_df['date'].dt.to_period('M')
time_df['quarter'] = time_df['date'].dt.to_period('Q')
去除重復(fù)值。
df.drop_duplicates(subset=['作者id'],keep='first',inplace=True)
數(shù)據(jù)格式批量轉(zhuǎn)換铭乾。
df['FWCI'] = df['FWCI'].astype(float)
去除帶有空值的行或列剪廉。
data_df.dropna(axis=0,how='any',inplace=True) # axis=0去除行,axis=1去除列