python下pandas的14个最佳特色功能

上传人:第*** 文档编号:30558434 上传时间:2018-01-30 格式:DOCX 页数:24 大小:407.04KB
返回 下载 相关 举报
python下pandas的14个最佳特色功能_第1页
第1页 / 共24页
python下pandas的14个最佳特色功能_第2页
第2页 / 共24页
python下pandas的14个最佳特色功能_第3页
第3页 / 共24页
python下pandas的14个最佳特色功能_第4页
第4页 / 共24页
python下pandas的14个最佳特色功能_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《python下pandas的14个最佳特色功能》由会员分享,可在线阅读,更多相关《python下pandas的14个最佳特色功能(24页珍藏版)》请在金锄头文库上搜索。

1、Python下Pandas的14个最佳特色功能14 Best Python Pandas FeaturesPandas is the most widely used tool for data munging. It contains high-level data structures and manipulation tools designed to make data analysis fast and easy.In this post, I am going to discuss the most frequently used pandas features. I will b

2、e using olive oil data set for this tutorial, you can download the data set from this page (scroll down to data section). Apart from serving as a quick reference, I hope this post will help you to quickly start extracting value from Pandas. So lets get started!1) Loading Data“The Olive Oils data set

3、 has eight explanatory variables (levels of fatty acids in the oils) and nine classes(areas of Italy)”. For more information you can check my Ipython notebook.I am importing numpy, pandas and matplotlib modules.1234%matplotlib inlineimport numpy as npimport matplotlib.pyplot as pltimport pandas as p

4、dI am using pd.read_csv to load olive oil data set. Function head returns the first n rows of olive.csv. Here I am returning the first 5 rows. 2) Rename FunctionI am going to rename the first column (Unnamed: 0) to area_Idili.Rename function as an argument it takes a dictionary of column names that

5、should be renamed as keys(olive_oil.columns0) and the new title(area_Idili) to be the value. Olive_oil.columns will return the column names. inplace = True is used in case you want to modify the existing DataFrame. 3) MapOne thing that I want to do is to clean the area_Idli column and remove the num

6、bers. I am using map object to perform this operation. Map property applies changes to every element of a column. I am applying split function to column area_idili. Split function returns a list, and -1 returns the last element of the list. A detailed explanation of lambda is given here. See how spl

7、it function works:4) Apply and Apply MapI have a list of acids called acidlist. Apply is a pretty flexible function, it applies a function along any axis of the DataFrame. I will be using applyfunction to divide each value of the acid by 100.list_of_acids =palmitic, palmitoleic, stearic, oleic, lino

8、leic, linolenic, arachidic, eicosenoic12df = olive_oillist_of_acids.apply (lambda x: x/100.00)df.head (5)Similar to apply, apply map function works element-wise on a DataFrame.Summing up, apply works on a row/column basis of a DataFrame,applymap works element-wise on a DataFrame, and map works eleme

9、nt-wise on a Series.5) Shape and ColumnsShape property will return a tuple of the shape of the data frame.olive_oil.columns will give you the column values. 6) Unique functionOlive_oil.region.unique() will return unique entries in region column, there are three unique regions (1,2,3). I am applying

10、the same unique property to area column, there are 9 unique areas.7) Cross TabCross Tab computes the simple cross tabulation of two factors. Here I am applying cross tabulation to area and region columns. 8) Accessing Sub data framesThe syntax for indexing multiple columns is given below.To index a

11、single column you can use olive_oilpalmitic orolive_oil.palmitic.9) Plottingplt.hist(olive_oil.palmitic). You can plot histogram using plt.hist function. You can also generate subplots of pandas data frame. Here I am generating 4 different subplots for palmitic and linolenic columns. You can set the

12、 size of the figure using figsize object, nrows and ncols are nothing but the number of columns and rows.10) Groupby and StatisticsGroupby groups the data into 3 parts(region 1, 2 and 3). The functiongroupby gives dictionary like object. Here I am grouping by regions olive_oil.groupby(region).I am a

13、pplying describe on the group, describe takes any data frame and compute statistics on it. This is the quick way of getting statistics by group of any data frame.You can also calculate standard deviation of the region_groupby using olive_oil.groupby(region).std()11) Aggregate functionAggregate funct

14、ion takes a function as an argument and applies the function to columns in the groupby sub dataframe. I am applying np.mean(computes mean) on all three regions. 12) JoinI am renaming ol mean and olstd columns.In 34: list_of_acids =palmitic, palmitoleic, stearic, oleic, linoleic, linolenic, arachidic

15、, eicosenoicPandas can do general merges. When we do that along an index, its called a join. Here I make two sub-data frames and join them on the common region index.13) MaskingYou can also mask a particular part of the data frame.olive_oil.eicosenoic 0.05 will check if each value in column eicoseno

16、ic is less than 0.05, if the value is less than 0.05 then it will return true, else it will return false.In 29: eico=(olive_oil.eicosenoic 0.05)14) Handling Missing ValuesMissing data is common in most data analysis applications. I find drop na and fill na function very useful while handling missing data.I am creating

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号