python

代码在jupyterNotebook中运行成功,过程中需要的包,已经安装完毕,每一代码格代表一个单元的代码,如需Pycharm运行,适当修改代码

# 定义函数,该函数主要用来分解信息,content为待处理内容,separator为拆分节点
def list_split(content,separator):  #分解信息函数
    new_list=[]
    for i in range(len(content)):
        new_list.append(list(filter(None,content[i].split(separator)))) # 此处采用了Python内置对象filter过滤器,对,后的数据实施,前的执行操作
    return new_list

# 定义函数,该函数主要用来分解信息,content为待处理内容,separator为拆分节点
def list_split(content,separator):  #分解信息函数
    new_list=[]
    for i in range(len(content)):
        new_list.append(list(filter(None,content[i].split(separator)))) # 此处采用了Python内置对象filter过滤器,对,后的数据实施,前的执行操作
    return new_list

# 将WXdata['Organ']中的信息,采用分号separator,将信息分解开,以此获得学校信息名称,最后得出排名前10的学校名称
organ= list_split(WXdata['Organ'],';')
# 获得学校信息名称之后,统计学校计量
data1=pd.DataFrame([[i,search_university(organ,i)] for i in university['学校名称']])
data1.rename(columns={0:'学校名称',1:'频数'},inplace=True)
data1.sort_values(by='频数',ascending = False)[:10]

# 同样上述方法,获取基金的计量信息,返回排名前10的基金
jijin=list_split(WXdata['Fund'].dropna(axis=0,how='all').tolist(),';;')
data2=pd.DataFrame([[i,search_university(jijin,i)] for i in fund['基金名称']])
data2.rename(columns={0:'学校名称',1:'频数'},inplace=True)
data2.sort_values(by='频数',ascending = False)[:10]

# 同样上述方法,统计文献中关键词的计量信息
keyword=list_split(WXdata['Keyword'].dropna(axis=0,how='all').tolist(),';;')
keyword1=sum(keyword,[])
pd.DataFrame(keyword1)[0].value_counts()[:10]

# 定义新的函数,用新的标识来替代目标信息中的某些内容
def list_replace(content,old,new): 
    return [content[i].replace(old,new) for i in range(len(content))]

# 使用上述函数,将Author列中数据进行过滤后,将,换为;
author=list_replace(WXdata['Author'].dropna(axis=0,how='all').tolist(),',',';')# dropna主要用于滤除缺失数据,传入how='all'参数后将只丢弃全为缺失值的那些行,axis=1是对列进行计算 axiis=0是行
# 
author1=list_split(author,';');author1
# type(author1) # 用来查看author的类型,为列表list类型,下同
author2=sum(author1,[])  # 这里采用sum与列表推导式方法,将author1列表中所有列表写入author2中,汇总成为一个列表
# type(author2)
pd.DataFrame(author2)[0].value_counts()[:10]  # 此处为何要有前面的[0],见详解

详解

# 统计期刊与年份计量信息
WXdata.Source.value_counts()[:10]  # 统计WXdata中Source列的统计信息,仅显示前10个
WXdata.Year.value_counts()

# 构造作者共现矩阵,机构共现矩阵,关键词共现矩阵
import networkx as nx
organ=list_split(WXdata['Organ'],';')
data1=pd.DataFrame([[i,search_university(organ,i)] for i in university['学校名称']])
data1.rename(columns={0:'学校名称',1:'频数'},inplace=True)
keyword=list_split(WXdata['Keyword'].dropna(axis=0,how='all').tolist(),';;')
keyword1=sum(keyword,[])
author=list_replace(WXdata['Author'].dropna(axis=0,how='all').tolist(),',',';')
author1=list_split(author,';')
author2=sum(author1,[])
#获取前30名的高频数据
data_author=pd.DataFrame(author2)[0].value_counts()[:30].index.tolist();data_author
data_keyword=pd.DataFrame(keyword1)[0].value_counts()[0:30].index.tolist();data_keyword
data_university=data1.sort_values(by='频数',ascending = False)[0:30]['学校名称'].tolist()

def occurence(data,document):  #生成共现矩阵
    empty1=[];empty2=[];empty3=[]
    for a in data:
        for b in data:
            count = 0
            for x in document:
                if  [a in i for i in x].count(True) >0 and [b in i for i in x].count(True) >0:
                        count += 1
            empty1.append(a);empty2.append(b);empty3.append(count)
    df=pd.DataFrame({'from':empty1,'to':empty2,'weight':empty3})
    G=nx.from_pandas_edgelist(df, 'from', 'to', 'weight')
    return (nx.to_pandas_adjacency(G, dtype=int))
Matrix1=occurence(data_author,author1);Matrix1
Matrix2=occurence(data_university,organ)
Matrix3=occurence(data_keyword,keyword)
Matrix1

# 绘制作者共现矩阵
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.rcParams['axes.unicode_minus']=False
graph1=nx.from_pandas_adjacency(Matrix1)
nx.draw(graph1,with_labels=True,node_color='yellow')

python_第1张图片

#绘制机构共现矩阵
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.rcParams['axes.unicode_minus']=False
graph2=nx.from_pandas_adjacency(Matrix2)
nx.draw(graph2,with_labels=True,node_color='yellow')

# 绘制关键词共现矩阵
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.rcParams['axes.unicode_minus']=False
graph3=nx.from_pandas_adjacency(Matrix3)
nx.draw(graph3,with_labels=True,node_color='yellow')


# 进行层次聚类,绘图
import scipy.cluster.hierarchy as sch
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.rcParams['axes.unicode_minus']=False
H1=sch.linkage(Matrix3,method='ward')
sch.dendrogram(H1,labels=Matrix3.index,orientation='right')

你可能感兴趣的:(python,python,开发语言)