pandas使用小结(二)

2 篇文章 0 订阅
1 篇文章 0 订阅

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]]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值