打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Pandas 数据框增、删、改、查、去重、抽样基本操作

总括

pandas的索引函数主要有三种:
loc 标签索引,行和列的名称
iloc 整型索引(绝对位置索引),绝对意义上的几行几列,起始索引为0
ix 是 iloc 和 loc的合体
at是loc的快捷方式
iat是iloc的快捷方式

建立测试数据集:

import pandas as pddf = pd.DataFrame({'a': [1, 2, 3], 'b': ['a', 'b', 'c'],'c': ["A","B","C"]})print(df)   a  b  c0  1  a  A1  2  b  B2  3  c  C
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

行操作

选择某一行

print(df.loc[1,:])a    2b    bc    BName: 1, dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5

选择多行

print(df.loc[1:2,:])#选择1:2行,slice为1   a  b  c1  2  b  B2  3  c  Cprint(df.loc[::-1,:])#选择所有行,slice为-1,所以为倒序   a  b  c2  3  c  C1  2  b  B0  1  a  Aprint(df.loc[0:2:2,:])#选择0至2行,slice为2,等同于print(df.loc[0:2:2,:])因为只有3行   a  b  c0  1  a  A2  3  c  C
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

条件筛选

普通条件筛选

print(df.loc[:,"a"]>2)#原理是首先做了一个判断,然后再筛选0    False1    False2     TrueName: a, dtype: boolprint(df.loc[df.loc[:,"a"]>2,:])   a  b  c2  3  c  C
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

另外条件筛选还可以集逻辑运算符 | for or, & for and, and ~for not

In [129]: s = pd.Series(range(-3, 4))In [132]: s[(s < -1) | (s > 0.5)]Out[132]: 0   -31   -24    15    26    3dtype: int64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

isin

非索引列使用isin

In [141]: s = pd.Series(np.arange(5), index=np.arange(5)[::-1], dtype='int64')In [143]: s.isin([2, 4, 6])Out[143]: 4    False3    False2     True1    False0     Truedtype: boolIn [144]: s[s.isin([2, 4, 6])]Out[144]: 2    20    4dtype: int64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

索引列使用isin

In [145]: s[s.index.isin([2, 4, 6])]Out[145]: 4    02    2dtype: int64# compare it to the followingIn [146]: s[[2, 4, 6]]Out[146]: 2    2.04    0.06    NaNdtype: float64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

结合any()/all()在多列索引时

In [151]: df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': ['a', 'b', 'f', 'n'],   .....:                    'ids2': ['a', 'n', 'c', 'n']})   .....: In [156]: values = {'ids': ['a', 'b'], 'ids2': ['a', 'c'], 'vals': [1, 3]}In [157]: row_mask = df.isin(values).all(1)In [158]: df[row_mask]Out[158]:   ids ids2  vals0   a    a     1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

where()

In [1]: dates = pd.date_range('1/1/2000', periods=8)In [2]: df = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])In [3]: dfOut[3]:                    A         B         C         D2000-01-01  0.469112 -0.282863 -1.509059 -1.1356322000-01-02  1.212112 -0.173215  0.119209 -1.0442362000-01-03 -0.861849 -2.104569 -0.494929  1.0718042000-01-04  0.721555 -0.706771 -1.039575  0.2718602000-01-05 -0.424972  0.567020  0.276232 -1.0874012000-01-06 -0.673690  0.113648 -1.478427  0.5249882000-01-07  0.404705  0.577046 -1.715002 -1.0392682000-01-08 -0.370647 -1.157892 -1.344312  0.844885In [162]: df.where(df < 0, -df)Out[162]:                    A         B         C         D2000-01-01 -2.104139 -1.309525 -0.485855 -0.2451662000-01-02 -0.352480 -0.390389 -1.192319 -1.6558242000-01-03 -0.864883 -0.299674 -0.227870 -0.2810592000-01-04 -0.846958 -1.222082 -0.600705 -1.2332032000-01-05 -0.669692 -0.605656 -1.169184 -0.3424162000-01-06 -0.868584 -0.948458 -2.297780 -0.6847182000-01-07 -2.670153 -0.114722 -0.168904 -0.0480482000-01-08 -0.801196 -1.392071 -0.048788 -0.808838
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

DataFrame.where() differs from numpy.where()的区别

In [172]: df.where(df < 0, -df) == np.where(df < 0, df, -df)
  • 1

当series对象使用where()时,则返回一个序列

In [141]: s = pd.Series(np.arange(5), index=np.arange(5)[::-1], dtype='int64')In [159]: s[s > 0]Out[159]: 3    12    21    30    4dtype: int64In [160]: s.where(s > 0)Out[160]: 4    NaN3    1.02    2.01    3.00    4.0dtype: float64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

抽样筛选

DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)
当在有权重筛选时,未赋值的列权重为0,如果权重和不为1,则将会将每个权重除以总和。random_state可以设置抽样的种子(seed)。axis可是设置列随机抽样。

In [105]: df2 = pd.DataFrame({'col1':[9,8,7,6], 'weight_column':[0.5, 0.4, 0.1, 0]})In [106]: df2.sample(n = 3, weights = 'weight_column')Out[106]:    col1  weight_column1     8            0.40     9            0.52     7            0.1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

增加行

df.loc[3,:]=4     a  b  c0  1.0  a  A1  2.0  b  B2  3.0  c  C3  4.0  4  4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

插入行

pandas里并没有直接指定索引的插入行的方法,所以要自己设置

line = pd.DataFrame({df.columns[0]:"--",df.columns[1]:"--",df.columns[2]:"--"},index=[1])df = pd.concat([df.loc[:0],line,df.loc[1:]]).reset_index(drop=True)#df.loc[:0]这里不能写成df.loc[0],因为df.loc[0]返回的是series     a  b  c0  1.0  a  A1  --  -- --2  2.0  b  B3  3.0  c  C4  4.0  4  4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

交换行

df.loc[[1,2],:]=df.loc[[2,1],:].values   a  b  c0  1  a  A1  3  c  C2  2  b  B
  • 1
  • 2
  • 3
  • 4
  • 5

删除行

df.drop(0,axis=0,inplace=True)print(df)   a  b  c1  2  b  B2  3  c  C
  • 1
  • 2
  • 3
  • 4
  • 5

注意

在以时间作为索引的数据框中,索引是以整形的方式来的。

In [39]: dfl = pd.DataFrame(np.random.randn(5,4), columns=list('ABCD'), index=pd.date_range('20130101',periods=5))In [40]: dflOut[40]:                    A         B         C         D2013-01-01  1.075770 -0.109050  1.643563 -1.4693882013-01-02  0.357021 -0.674600 -1.776904 -0.9689142013-01-03 -1.294524  0.413738  0.276662 -0.4720352013-01-04 -0.013960 -0.362543 -0.006154 -0.9230612013-01-05  0.895717  0.805244 -1.206412  2.565646In [41]: dfl.loc['20130102':'20130104']Out[41]:                    A         B         C         D2013-01-02  0.357021 -0.674600 -1.776904 -0.9689142013-01-03 -1.294524  0.413738  0.276662 -0.4720352013-01-04 -0.013960 -0.362543 -0.006154 -0.923061
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

列操作

选择某一列

print(df.loc[:,"a"])0    11    22    3Name: a, dtype: int64
  • 1
  • 2
  • 3
  • 4
  • 5

选择多列

print(df.loc[:,"a":"b"])   a  b0  1  a1  2  b2  3  c
  • 1
  • 2
  • 3
  • 4
  • 5

增加列,如果对已有的列,则是赋值

df.loc[:,"d"]=4   a  b  c  d0  1  a  A  41  2  b  B  42  3  c  C  4
  • 1
  • 2
  • 3
  • 4
  • 5

交换两列的值

df.loc[:,['b', 'a']] = df.loc[:,['a', 'b']].valuesprint(df)   a  b  c0  a  1  A1  b  2  B2  c  3  C
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

删除列

1)直接del DF[‘column-name’]

2)采用drop方法,有下面三种等价的表达式:

DF= DF.drop(‘column_name’, 1);

DF.drop(‘column_name’,axis=1, inplace=True)

DF.drop([DF.columns[[0,1,]]], axis=1,inplace=True)

df.drop("a",axis=1,inplace=True)print(df)   b  c0  a  A1  b  B2  c  C
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

还有一些其他的功能:
切片df.loc[::,::]
选择随机抽样df.sample()
去重.duplicated()
查询.lookup

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
pandas小记:pandas索引和选择
十分钟搞定pandas
Python数据分析-Pandas
【学习笔记】python实现excel数据处理
一起学习Python常用模块——pandas
简约而不简单|值得收藏的Pandas基本操作指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服