您的位置:首页 > 编程语言 > Python开发

python自学笔记15之实例之绘图、dataframe操作、读写csv,excle

2016-10-19 17:02 891 查看

用Python绘图,借助强大的numpy和matplotlib

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
x = np.linspace(0,1)
y = np.sin(4*np.pi*x)*np.exp(-5*x)
t = pd.DataFrame(y,index = x)
t.plot()
plt.show()




用pandas写csv文件

from matplotlib.finance import quotes_historical_yahoo
from datetime import date
import pandas as pd
today = date.today()
start = (today.year-1,today.month,today.day)
quotes  = quotes_historical_yahoo('IBM',start,today)
df = pd.DataFrame(quotes)
df.to_csv('stockIBM.csv')


从工作目录下可以看到多了个stockIBM.csv的文件,需要注意的是:

MatplotlibDeprecationWarning: This function has been deprecated in 1.4 in favor of
quotes_historical_yahoo_ochl
, which maintains the original argument order, or
quotes_historical_yahoo_ohlc
, which uses the open-high-low-close order. This function will be removed in 1.5

mplDeprecation)


意味着从matplotlib.finance中导入quotes_historical_yahool的时候出现了警告,

从matplotlib的官方帮助文档:这里,可以看到:

This module is deprecated in 1.4 and will be moved to mpl_toolkits or it’s own project in the future.

matplotlib.finance模块在1.4中不支持有其他变动,其他更详细的文档参看help

读取csv:

result = pd.read_csv('stockIBM.csv')


单独一列的读取显示:

print(result['1'])
0     144.305395
1     137.217722
2     135.060600
3     136.495473
4     139.259276
5     139.394094
6     138.199971
7     132.816810
8     135.166530
9     135.243571
10    135.301350
11    134.839113
12    137.275500
13    136.370279
14    134.723651
...
238    156.990005
239    158.630005
240    158.899994
241    158.059998
242    157.669998
243    157.070007
244    156.839996
245    157.139999
246    156.710007
247    156.729996
248    154.970001
249    153.699997
250    154.470001
251    154.449997
252    150.020004
Name: 1, Length: 253, dtype: float64


注意不是索引是列名

创建一个DataFrame读入singer.csv

from matplotlib.finance import quotes_historical_yahoo
from datetime import date
import pandas as pd
df = pd.DataFrame({'singer':['the rolling stones','beatless','guns n roses','metallica'],'song':['satisfaction','let it be','dont cry','nothing else matters']})
df.to_csv('singer.csv')
result = pd.read_csv('singer.csv')
print(result['singer'])

0    the rolling stones
1              beatless
2          guns n roses
3             metallica
Name: singer, dtype: object


dataframe简单操作

列与列求和直接用+,赋值也是直接赋,千万别想太多

data = {'number':[1001,1002,1003],'name':\
['xiaoming','xiaohong','xiaohua'],'python':\
[77,88,99],'math':[87,82,91]}
df = pd.DataFrame(data)
df['sum'] = df['python']+df['math']


注意append的使用,dataframe.append是添加行

注意DataFrame的生成方式,里面用了一个dict类型

读写excel

df.to_excel(‘grade.xlsx’)

pd.read_excel(‘grade.xlse’)

和csv类似

pandas官方文档:help
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python csv numpy 实例