Pandas常用操作

来源:互联网 发布:婴儿面膜淘宝授权书 编辑:程序博客网 时间:2024/06/06 16:35

Pandas是一款基于Python的数据分析包,使用起来非常方便,能够极大地解放数据分析师的繁琐工作,强烈推荐!

Series

import pandas as pd

Series类说明

class Series(pandas.core.base.IndexOpsMixin, pandas.core.generic.NDFrame)
| One-dimensional ndarray with axis labels (including time series).

help(pd.Series(a[1:,0]).map)
Examples

>>> xone   1two   2three 3>>> y1  foo2  bar3  baz>>> x.map(y)one   footwo   barthree baz
>>>pd.Series(a[1:,0])0    41    7dtype: int64>>>pd.Series(a[1:,0]).map(lambda x: "|"+str(x)+"|")0    |4|1    |7|dtype: object>>> pda   c1  c20   1   41   2   52   3   6>>> pdb   c1  c20  10   71   9   62   8   5>>> pda+10+pdb   c1  c20  21  211  21  212  21  21

Series的bool运算选择

>>> pda[pda>2]2    33    44    5dtype: int64

复杂一些的

>>> ser = pd.Series(['111', '112', '122'])>>> ser0    1111    1122    122dtype: object
>>> ser[[x.startswith('11') for x in ser]]0    1111    112dtype: object这里是不能简单的使用ser[ser.startswith(‘11’)], 因为ser不是str类型

DataFrame

DataFrame的常用构造方式:

  • 使用Series组合构造
  • 直接从csv文件中读取pd.read_csv(“filename.csv”)
  • 多维数组构造,
    df = pd.DataFrame(np.random.randn(10, 4), columns=[‘A’, ‘B’, ‘C’, ‘D’])

构造完数据后,存储至磁盘文件系统

df = pd.DataFrame({        'order_adslot': order_adslot_ds,        'weight': weight    })    df.to_csv('data/gm_ranking_model.dat', index=False, sep='\t')

给已有的dataframe增加一行数据

>>> df = df.append(pd.DataFrame({"c1":[100], "c2":[200]}))>>> df    c1   c20    1   101    2    92    3    83    4    74    5    65    1   100  100  200

分组求和(聚合操作)

>>> df.groupby('c1').sum()    或者df.groupby([‘c1']).sum()      c2c11     202      93      84      75      6100  200As you can see, the result of the aggregation will have the group names as the new index along the grouped axis>>> df.groupby([‘c1’], as_index=False).sum()

列与列之间的四则运算

>>> df   c1  c20   1   11   2   12   3   43   4   44   5   5>>> df['c3'] = df.c1+df.c2>>> df   c1  c2  c30   1   1   21   2   1   32   3   4   73   4   4   84   5   5  10

删除某一列

>>> del df['c3']>>> df   c1  c20   1   11   2   12   3   43   4   44   5   5

bool运算,选择满足条件的行

>>> df[df.c1>=3]   c1  c2  c32   3   4   73   4   4   84   5   5  10>>> df[df["c1"]>=3]   c1  c2  c32   3   4   73   4   4   84   5   5  10

插一个奇怪的问题:

>>> df    c1        c2                                           c3   c40  昂科拉  10-14-15  太平洋汽车移动PCauto手机客户端\n(IOS&Android)资讯/图文列表第十位    11    a         b                                            c  NaN>>> df[[math.isnan(float(x)) for x in df["c4"]]]  c1 c2 c3   c41  a  b  c  NaN>>> df[[math.isnan(float(x)) for x in df["c4"] if type(x) is float]]这个会报错的,原因不明;但是金勇提供了另一种方式>>> df[df[“c4”].isnull()]那如果我们要选出不为空的该怎么做呢?>>> df[df[“c4”].isnull() is False]    #这个也是会报错的 金勇提供了另一种方式>>> df = df.dropna()dropna()函数是删除至少有一列为空的行或所有列均为空的行如果我们需要指定某一列不为空的行,就需要使用notnull()>>>df[df[“c4”].notnull()]

按位置选定指定的行和列

>>> df.iloc[:, [1,2]]   c2  c30   1   21   1   32   4   73   4   84   5  10

深复制&浅复制

df2 = df.copy()>>> del df2["c3"]>>> df   c1  c2  c30   1   1   21   2   1   32   3   4   73   4   4   84   5   5  10>>> df2   c1  c20   1   11   2   12   3   43   4   44   5   5

DataFrame与DataFrame之间的join操作

>>> df2   c1  c2  c40   1   1   11   2   1   22   3   4  123   4   4  164   5   5  25>>> df.merge(df2)   c1  c2  c3  c40   1   1   2   11   2   1   3   22   3   4   7  123   4   4   8  164   5   5  10  25

参考链接:http://pandas.pydata.org/pandas-docs/stable/api.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 退换东西的时候外包装坏了怎么办 闲鱼买家以与描述不符退货怎么办 闲鱼买家申请退款又签收了怎么办 店铺买的手机是拆开过的怎么办 卖家以包装破损拒收快递怎么办 车辆迁出落户信封被自己拆开怎么办 快递签收了发现物品坏了怎么办 快递签收后发现货坏了怎么办 闲鱼退货有破损卖家要求赔偿怎么办 邮政快递收货后发现快递破损怎么办 中通把包裹发到别的省份去了怎么办 寄快递没拿单子但东西破损了怎么办 快递签收以后发现东西少了怎么办 淘宝时收到的货破损我该怎么办 淘宝买的冰箱七天无理由退货怎么办 截图放进ai中变得很模糊怎么办 买房子的贷款合同丢了怎么办 索尼9000e挂墙后面借口怎么办 想把木头锯成小块的怎么办 第一天上班交接完工作想辞职怎么办 POS机刷卡成功没打出单子怎么办 建行买的理财保险单子丢了怎么办 退休老师饭卡的钱突然少了怎么办 背驼得厉害头低的厉害怎么办 被别人举报自己碰瓷了怎么办 苹果平板微信屏幕不是全屏怎么办 大冒险告白被接受了怎么办在线阅读 手机屏锁图案密码忘了怎么办 opp手机锁屏密码忘了怎么办 宝宝的玩具里面脏了洗不到怎么办 早上起来眼睛被眼屎粘住了怎么办 魔兽世界神器满级任务放弃了怎么办 魔兽世界神器满级任务没接到怎么办 情侣空间上的照片删了怎么办 微信好友群群主把你踢出房间怎么办 我被车撞了对方把现场破坏该怎么办 美术生专业分可以文化分不行怎么办 小狗又吐又拉不吃东西怎么办 狗狗把脖子挠破了怎么办 几分钟我的手机疯狂收到短信怎么办 初中孩子沉迷动漫及画动漫画怎么办