日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python怎么处理数据_python panda怎么处理数据

發布時間:2025/3/20 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么处理数据_python panda怎么处理数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

匿名用戶

1級

2016-08-22 回答

創建數據

通過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"業績" ])

In [16]: data

Out[16]:

姓名 業績

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]:

姓名 業績

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"銷售業績"])

將data導出到out.csv文件里,index參數是指是否有主索引,header如果不指定則是以data里columns為頭,如果指定則是以后邊列表里的字符串為表頭,但要注意的是header后的字符串列表的個數要和data里的columns字段個數相同。

可到c盤用Notepad++打開out.csv看看。

簡單的數據分析

In [43]: data

Out[43]:

姓名 業績

0 hello 244937

1 the 132008

2 cruel 278446

3 world 613409

4 en 799201

#排序并取前三名

In [46]: Sorted = data.sort([u"業績"], ascending=False)

Sorted.head(3)

Out[46]:

姓名 業績

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"業績"].plot()

MaxValue = df[u"業績"].max()

MaxName = df[u"姓名"][df[u"業績"] == df[u"業績"].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怎么处理数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。