pandas使用小结(二)

1.创建dataFrame的几种方法[1]

方法一

传递一个list对象来创建一个Series

方法二

传递一个numpy array,时间索引以及列标签来创建一个DataFrame

方法三

传递一个能够被转换成类似序列结构的字典对象来创建一个DataFrame

2.查看dataFrame的数据类型[1]

dataFrame.dtypes

如果IPYTHON的话,dataFrame.即可

3.对dataframe中数据的快速统计命令[1]

dataFrame.describe(),会统计出各列的:计数,平均数,方差,最小值,最大值,以及quantile数值

4.对dataFrame中数据按轴排序,按值排序:[1]

dataframe.sort_index(axis,ascending=False)

dataframe.sort(column='B')

5.切片[1]

选  (1) 择一个单独的列:df['A'],这将会返回一个Series,等同于df.A

    (2) 对行切片,df[0:3],这将会返回1-3行的数据所组成的dataframe

6.对dataframe求相关系数矩阵的命令是dataframe.corr(),协方差矩阵的命令是dataframe.cov()[2]

7.求dataframe中两列的相关系数,命令是dataframe.列1.corr(dataframe.列2),协方差的命令是dataframe.列1.cov(dataframe.列2)[2]

8.通过标签切片

dataframe.loc['20001231',['A','B']],其中20001231是index,A,B是列名

9.通过位置切片

dataframe.iloc[1,3]或者dataframe.iloc[1:3,3:5]或者dataframe.iloc[[1,2],[1,2,3]]

10.使用类似SQL中where来切片

dataFrame[dataFrame['pn']=='5781986']

11.绘图时候怎样添加子图:

You can manually create the subplots with matplotlib, and then plot the dataframes on a specific subplot using the ax keyword. For example for 4 subplots (2x2):

import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2, ncols=2) df1.plot(ax=axes[0,0]) df2.plot(ax=axes[0,1]) ...

Here axes is an array which holds the different subplot axes, and you can access one just by indexing axes.
If you want a shared x-axis, then you can provide sharex=True to plt.subplots.

12.对dataframe中元素,进行类型转换

df['2nd'] = df['2nd'].str.replace(',','').astype(int) df['CTR'] = df['CTR'].str.replace('%','').astype(np.float64)

13.调换np多维数组的index顺序

>>>import numpy as np
>>> x = np.arange(12).reshape(2,2,3)
>>> x
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]])
>>> x_ = np.transpose(x,(2,0,1))
>>> x_
array([[[ 0,  3],
        [ 6,  9]],

       [[ 1,  4],
        [ 7, 10]],

       [[ 2,  5],
        [ 8, 11]]])

猜你喜欢

转载自blog.csdn.net/doubleicefire/article/details/80175126