简介
明天咱们会解说一下Pandas的高级教程,包含读写文件、选取子集和图形示意等。
读写文件
数据处理的一个关键步骤就是读取文件进行剖析,而后将剖析处理结果再次写入文件。
Pandas反对多种文件格式的读取和写入:
In [108]: pd.read_ read_clipboard() read_excel() read_fwf() read_hdf() read_json read_parquet read_sas read_sql_query read_stata read_csv read_feather() read_gbq() read_html read_msgpack read_pickle read_sql read_sql_table read_table
接下来咱们会以Pandas官网提供的Titanic.csv为例来解说Pandas的应用。
Titanic.csv提供了800多个泰坦利特号上乘客的信息,是一个891 rows x 12 columns的矩阵。
咱们应用Pandas来读取这个csv:
In [5]: titanic=pd.read_csv("titanic.csv")
read_csv办法会将csv文件转换成为pandas 的DataFrame
。
默认状况下咱们间接应用DF变量,会默认展现前5行和后5行数据:
In [3]: titanicOut[3]: PassengerId Survived Pclass Name Sex ... Parch Ticket Fare Cabin Embarked0 1 0 3 Braund, Mr. Owen Harris male ... 0 A/5 21171 7.2500 NaN S1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female ... 0 PC 17599 71.2833 C85 C2 3 1 3 Heikkinen, Miss. Laina female ... 0 STON/O2. 3101282 7.9250 NaN S3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female ... 0 113803 53.1000 C123 S4 5 0 3 Allen, Mr. William Henry male ... 0 373450 8.0500 NaN S.. ... ... ... ... ... ... ... ... ... ... ...886 887 0 2 Montvila, Rev. Juozas male ... 0 211536 13.0000 NaN S887 888 1 1 Graham, Miss. Margaret Edith female ... 0 112053 30.0000 B42 S888 889 0 3 Johnston, Miss. Catherine Helen "Carrie" female ... 2 W./C. 6607 23.4500 NaN S889 890 1 1 Behr, Mr. Karl Howell male ... 0 111369 30.0000 C148 C890 891 0 3 Dooley, Mr. Patrick male ... 0 370376 7.7500 NaN Q[891 rows x 12 columns]
能够应用head(n)和tail(n)来指定特定的行数:
In [4]: titanic.head(8)Out[4]: PassengerId Survived Pclass Name Sex ... Parch Ticket Fare Cabin Embarked0 1 0 3 Braund, Mr. Owen Harris male ... 0 A/5 21171 7.2500 NaN S1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female ... 0 PC 17599 71.2833 C85 C2 3 1 3 Heikkinen, Miss. Laina female ... 0 STON/O2. 3101282 7.9250 NaN S3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female ... 0 113803 53.1000 C123 S4 5 0 3 Allen, Mr. William Henry male ... 0 373450 8.0500 NaN S5 6 0 3 Moran, Mr. James male ... 0 330877 8.4583 NaN Q6 7 0 1 McCarthy, Mr. Timothy J male ... 0 17463 51.8625 E46 S7 8 0 3 Palsson, Master. Gosta Leonard male ... 1 349909 21.0750 NaN S[8 rows x 12 columns]
应用dtypes能够查看每一列的数据类型:
In [5]: titanic.dtypesOut[5]: PassengerId int64Survived int64Pclass int64Name objectSex objectAge float64SibSp int64Parch int64Ticket objectFare float64Cabin objectEmbarked objectdtype: object
应用to_excel能够将DF转换为excel文件,应用read_excel能够再次读取excel文件:
In [11]: titanic.to_excel('titanic.xlsx', sheet_name='passengers', index=False)In [12]: titanic = pd.read_excel('titanic.xlsx', sheet_name='passengers')
应用info()能够来对DF进行一个初步的统计:
In [14]: titanic.info()<class 'pandas.core.frame.DataFrame'>RangeIndex: 891 entries, 0 to 890Data columns (total 12 columns):PassengerId 891 non-null int64Survived 891 non-null int64Pclass 891 non-null int64Name 891 non-null objectSex 891 non-null objectAge 714 non-null float64SibSp 891 non-null int64Parch 891 non-null int64Ticket 891 non-null objectFare 891 non-null float64Cabin 204 non-null objectEmbarked 889 non-null objectdtypes: float64(2), int64(5), object(5)memory usage: 83.6+ KB
DF的抉择
抉择列数据
DF的head或者tail办法只能显示所有的列数据,上面的办法能够抉择特定的列数据。
In [15]: ages = titanic["Age"]In [16]: ages.head()Out[16]:0 22.01 38.02 26.03 35.04 35.0Name: Age, dtype: float64
每一列都是一个Series:
In [6]: type(titanic["Age"])Out[6]: pandas.core.series.SeriesIn [7]: titanic["Age"].shapeOut[7]: (891,)
还能够多选:
In [8]: age_sex = titanic[["Age", "Sex"]]In [9]: age_sex.head()Out[9]: Age Sex0 22.0 male1 38.0 female2 26.0 female3 35.0 female4 35.0 male
如果抉择多列的话,返回的后果就是一个DF类型:
In [10]: type(titanic[["Age", "Sex"]])Out[10]: pandas.core.frame.DataFrameIn [11]: titanic[["Age", "Sex"]].shapeOut[11]: (891, 2)
抉择行数据
下面咱们讲到了怎么抉择列数据,上面咱们来看看怎么抉择行数据:
抉择客户年龄大于35岁的:
In [12]: above_35 = titanic[titanic["Age"] > 35]In [13]: above_35.head()Out[13]: PassengerId Survived Pclass Name Sex ... Parch Ticket Fare Cabin Embarked1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female ... 0 PC 17599 71.2833 C85 C6 7 0 1 McCarthy, Mr. Timothy J male ... 0 17463 51.8625 E46 S11 12 1 1 Bonnell, Miss. Elizabeth female ... 0 113783 26.5500 C103 S13 14 0 3 Andersson, Mr. Anders Johan male ... 5 347082 31.2750 NaN S15 16 1 2 Hewlett, Mrs. (Mary D Kingcome) female ... 0 248706 16.0000 NaN S[5 rows x 12 columns]
应用isin抉择Pclass在2和3的所有客户:
In [16]: class_23 = titanic[titanic["Pclass"].isin([2, 3])]In [17]: class_23.head()Out[17]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S5 6 0 3 Moran, Mr. James male NaN 0 0 330877 8.4583 NaN Q7 8 0 3 Palsson, Master. Gosta Leonard male 2.0 3 1 349909 21.0750 NaN S
下面的isin等于:
In [18]: class_23 = titanic[(titanic["Pclass"] == 2) | (titanic["Pclass"] == 3)]
筛选Age不是空的:
In [20]: age_no_na = titanic[titanic["Age"].notna()]In [21]: age_no_na.head()Out[21]: PassengerId Survived Pclass Name Sex ... Parch Ticket Fare Cabin Embarked0 1 0 3 Braund, Mr. Owen Harris male ... 0 A/5 21171 7.2500 NaN S1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female ... 0 PC 17599 71.2833 C85 C2 3 1 3 Heikkinen, Miss. Laina female ... 0 STON/O2. 3101282 7.9250 NaN S3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female ... 0 113803 53.1000 C123 S4 5 0 3 Allen, Mr. William Henry male ... 0 373450 8.0500 NaN S[5 rows x 12 columns]
同时抉择行和列
咱们能够同时抉择行和列。
应用loc和iloc能够进行行和列的抉择,他们两者的区别是loc是应用名字进行抉择,iloc是应用数字进行抉择。
抉择age>35的乘客名:
In [23]: adult_names = titanic.loc[titanic["Age"] > 35, "Name"]In [24]: adult_names.head()Out[24]: 1 Cumings, Mrs. John Bradley (Florence Briggs Th...6 McCarthy, Mr. Timothy J11 Bonnell, Miss. Elizabeth13 Andersson, Mr. Anders Johan15 Hewlett, Mrs. (Mary D Kingcome) Name: Name, dtype: object
loc中第一个值示意行抉择,第二个值示意列抉择。
应用iloc进行抉择:
In [25]: titanic.iloc[9:25, 2:5]Out[25]: Pclass Name Sex9 2 Nasser, Mrs. Nicholas (Adele Achem) female10 3 Sandstrom, Miss. Marguerite Rut female11 1 Bonnell, Miss. Elizabeth female12 3 Saundercock, Mr. William Henry male13 3 Andersson, Mr. Anders Johan male.. ... ... ...20 2 Fynney, Mr. Joseph J male21 2 Beesley, Mr. Lawrence male22 3 McGowan, Miss. Anna "Annie" female23 1 Sloper, Mr. William Thompson male24 3 Palsson, Miss. Torborg Danira female[16 rows x 3 columns]
应用plots作图
怎么将DF转换成为多样化的图形展现呢?
要想在命令行中应用matplotlib作图,那么须要启动ipython的QT环境:
ipython qtconsole --pylab=inline
间接应用plot来展现一下下面咱们读取的乘客信息:
import matplotlib.pyplot as pltimport pandas as pdtitanic = pd.read_excel('titanic.xlsx', sheet_name='passengers')titanic.plot()
横坐标就是DF中的index,列坐标是各个列的名字。留神下面的列只展现的是数值类型的。
咱们只展现age信息:
titanic['Age'].plot()
默认的是柱状图,咱们能够转换图形的模式,比方点图:
titanic.plot.scatter(x="PassengerId",y="Age", alpha=0.5)
抉择数据中的PassengerId作为x轴,age作为y轴:
除了散点图,还反对很多其余的图像:
[method_name for method_name in dir(titanic.plot) if not method_name.startswith("_")]Out[11]: ['area', 'bar', 'barh', 'box', 'density', 'hexbin', 'hist', 'kde', 'line', 'pie', 'scatter']
再看一个box图:
titanic['Age'].plot.box()
能够看到,乘客的年龄大多集中在20-40岁之间。
还能够将抉择的多列别离作图展现:
titanic.plot.area(figsize=(12, 4), subplots=True)
指定特定的列:
titanic[['Age','Pclass']].plot.area(figsize=(12, 4), subplots=True)
还能够先画图,而后填充:
fig, axs = plt.subplots(figsize=(12, 4));
先画一个空的图,而后对其进行填充:
titanic['Age'].plot.area(ax=axs);axs.set_ylabel("Age");fig
应用现有的列创立新的列
有时候,咱们须要对现有的列进行变换,以失去新的列,比方咱们想增加一个Age2列,它的值是Age列+10,则能够这样:
titanic["Age2"]=titanic["Age"]+10;titanic[["Age","Age2"]].head()Out[34]: Age Age20 22.0 32.01 38.0 48.02 26.0 36.03 35.0 45.04 35.0 45.0
还能够对列进行重命名:
titanic_renamed = titanic.rename( ...: columns={"Age": "Age2", ...: "Pclass": "Pclas2"})
列名转换为小写:
titanic_renamed = titanic_renamed.rename(columns=str.lower)
进行统计
咱们来统计下乘客的平均年龄:
titanic["Age"].mean()Out[35]: 29.69911764705882
抉择中位数:
titanic[["Age", "Fare"]].median()Out[36]: Age 28.0000Fare 14.4542dtype: float64
更多信息:
titanic[["Age", "Fare"]].describe()Out[37]: Age Farecount 714.000000 891.000000mean 29.699118 32.204208std 14.526497 49.693429min 0.420000 0.00000025% 20.125000 7.91040050% 28.000000 14.45420075% 38.000000 31.000000max 80.000000 512.329200
应用agg指定特定的聚合办法:
titanic.agg({'Age': ['min', 'max', 'median', 'skew'],'Fare': ['min', 'max', 'median', 'mean']})Out[38]: Age Faremax 80.000000 512.329200mean NaN 32.204208median 28.000000 14.454200min 0.420000 0.000000skew 0.389108 NaN
能够应用groupby:
titanic[["Sex", "Age"]].groupby("Sex").mean()Out[39]: AgeSex female 27.915709male 30.726645
groupby所有的列:
titanic.groupby("Sex").mean()Out[40]: PassengerId Survived Pclass Age SibSp Parch Sex female 431.028662 0.742038 2.159236 27.915709 0.694268 0.649682 male 454.147314 0.188908 2.389948 30.726645 0.429809 0.235702
groupby之后还能够抉择特定的列:
titanic.groupby("Sex")["Age"].mean()Out[41]: Sexfemale 27.915709male 30.726645Name: Age, dtype: float64
能够分类进行count:
titanic["Pclass"].value_counts()Out[42]: 3 4911 2162 184Name: Pclass, dtype: int64
下面等同于:
titanic.groupby("Pclass")["Pclass"].count()
DF重组
能够依据某列进行排序:
titanic.sort_values(by="Age").head()Out[43]: PassengerId Survived Pclass Name Sex \803 804 1 3 Thomas, Master. Assad Alexander male 755 756 1 2 Hamalainen, Master. Viljo male 644 645 1 3 Baclini, Miss. Eugenie female 469 470 1 3 Baclini, Miss. Helene Barbara female 78 79 1 2 Caldwell, Master. Alden Gates male
依据多列排序:
titanic.sort_values(by=['Pclass', 'Age'], ascending=False).head()Out[44]: PassengerId Survived Pclass Name Sex Age \851 852 0 3 Svensson, Mr. Johan male 74.0 116 117 0 3 Connors, Mr. Patrick male 70.5 280 281 0 3 Duane, Mr. Frank male 65.0 483 484 1 3 Turkula, Mrs. (Hedwig) female 63.0 326 327 0 3 Nysveen, Mr. Johan Hansen male 61.0
抉择特定的行和列数据,上面的例子咱们将会抉择性别为女性的局部数据:
female=titanic[titanic['Sex']=='female']female_subset=female[["Age","Pclass","PassengerId","Survived"]].sort_values(["Pclass"]).groupby(["Pclass"]).head(2)female_subsetOut[58]: Age Pclass PassengerId Survived1 38.0 1 2 1356 22.0 1 357 1726 30.0 2 727 1443 28.0 2 444 1855 18.0 3 856 1654 18.0 3 655 0
应用pivot能够进行轴的转换:
female_subset.pivot(columns="Pclass", values="Age")Out[62]: Pclass 1 2 31 38.0 NaN NaN356 22.0 NaN NaN443 NaN 28.0 NaN654 NaN NaN 18.0726 NaN 30.0 NaN855 NaN NaN 18.0female_subset.pivot(columns="Pclass", values="Age").plot()
本文已收录于 http://www.flydean.com/02-python-pandas-advanced/
最艰深的解读,最粗浅的干货,最简洁的教程,泛滥你不晓得的小技巧等你来发现!
欢送关注我的公众号:「程序那些事」,懂技术,更懂你!