python怎么处理数据_python panda怎么处理数据
匿名用戶
1級
2016-08-22 回答
創(chuàng)建數據
通過Python的zip構造出一元組組成的列表作為DataFrame的輸入數據rec。
In [3]: import pandas as pd
In [4]: import random
In [5]: num = random.sample(xrange(10000, 1000000), 5)
In [6]: num
Out[6]: [244937, 132008, 278446, 613409, 799201]
In [8]: names = "hello the cruel world en".split()
In [9]: names
Out[9]: ['hello', 'the', 'cruel', 'world', 'en']
In [10]: rec = zip(names, num)
In [15]: data = pd.DataFrame(rec, columns = [u"姓名",u"業(yè)績" ])
In [16]: data
Out[16]:
姓名 業(yè)績
0 hello 244937
1 the 132008
2 cruel 278446
3 world 613409
4 en 799201
DataFrame方法函數的第一個參數是數據源,第二個參數columns是輸出數據表的表頭,或者說是表格的字段名。
導出數據csv
Windows平臺上的編碼問題,我們可以先做個簡單處理,是ipython-notebook支持utf8.
import sys
reload(sys)
sys.setdefaultencoding("utf8")
接下來可以數據導出了。
In [31]: data
Out[31]:
姓名 業(yè)績
0 hello 244937
1 the 132008
2 cruel 278446
3 world 613409
4 en 799201
#在ipython-note里后加問號可查幫助,q退出幫助
In [32]: data.to_csv?
In [33]: data.to_csv("c:\\out.csv", index = True, header = [u"雇員", u"銷售業(yè)績"])
將data導出到out.csv文件里,index參數是指是否有主索引,header如果不指定則是以data里columns為頭,如果指定則是以后邊列表里的字符串為表頭,但要注意的是header后的字符串列表的個數要和data里的columns字段個數相同。
可到c盤用Notepad++打開out.csv看看。
簡單的數據分析
In [43]: data
Out[43]:
姓名 業(yè)績
0 hello 244937
1 the 132008
2 cruel 278446
3 world 613409
4 en 799201
#排序并取前三名
In [46]: Sorted = data.sort([u"業(yè)績"], ascending=False)
Sorted.head(3)
Out[46]:
姓名 業(yè)績
4 en 799201
3 world 613409
2 cruel 278446
圖形輸出
In [71]: import matplotlib.pyplot as plt
#使ipython-notebook支持matplotlib繪圖
%matplotlib inline
In [74]: df = data
#繪圖
df[u"業(yè)績"].plot()
MaxValue = df[u"業(yè)績"].max()
MaxName = df[u"姓名"][df[u"業(yè)績"] == df[u"業(yè)績"].max()].values
Text = str(MaxValue) + " - " + MaxName
#給圖添加文本標注
plt.annotate(Text, xy=(1, MaxValue), xytext=(8, 0), xycoords=('axes fraction', 'data'), textcoords='offset points')
如果注釋掉plt.annotate這行
總結
以上是生活随笔為你收集整理的python怎么处理数据_python panda怎么处理数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 0x00000000指令引用的内存不能为
- 下一篇: 关系查询处理 查询优化 论文_每日论文3