用pandas读取excel并画图展示

初学python,啥都不懂。刚好有个数据想分析一下,便搜索了如何使用python处理Excel并展示,短短的一段代码浪费了很多时间,直接贴代码吧:

  • python dict orderedDict
  • pandas
  • xlrd

代码块

代码块语法遵循标准markdown代码,例如: 
python 
import pandas as pd 
import matplotlib 
matplotlib.use(‘TkAgg’) 
import matplotlib.pylab as plt 
import collections

#读取Excel,并且只需要第二列,第19列 
df=pd.read_excel(‘xxxxxx.xlsx’,usecols=[2,19]) 
print(df) #查看一下,其实读入之后是个DataPanel 
//将某一列去重,保留第一个出现的 
data=df.drop_duplicates([‘S_INFO_WINDCODE / Wind代码’],’first’) 
data=df 
corp={} 
for i in range(len(data)): 
company=df.iloc[i,0] 
date=df.iloc[i,1] 
date=str(date)[:4] 
# print(date) 
# 因为数据是20180808这种,numpy的int64类型,我想要按年统计 不知道有没有更好的方式 
if date in corp: 
corp[date]=corp[date]+1 
else: 
corp[date]=1 
od = collections.OrderedDict(sorted(corp.items())) # 按年排序 
print(od.items()) 
fig, ax = plt.subplots() 
rect=ax.bar(od.keys(), od.values(), color=’b’) 
def autolabel(rects): 
“”” 
Attach a text label above each bar displaying its height 
“”” 
for rect in rects: 
height = rect.get_height() 
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
‘%d’ % int(height), 
ha=’center’, va=’bottom’)

autolabel(rect) 
plt.show()

脚注

目前只是按年统计,其实我还想按月统计,按更多其他维度来,还在学习中

你可能感兴趣的:(python,数据分析)