pandas 获得行列数,shuffle 函数sample() ,重建索引,DataFrame数据筛选——loc,iloc,at,iat

#pandas获取数据行数和列数,并非是用len或者length的,而是用shape:
Count_Row=df.shape[0] #gives number of row count
Count_Col=df.shape[1] #gives number of col count

pandas数据去重,链接,下面函数实现了从“CRASHSEV”中选出1,2,3,4的属性,乱序,然后取出前10000行,按行链接成新的数据,重建索引

def unbanlance(un_data):
    data1 = un_data.loc[(data["CRASHSEV"] == 1)].sample(frac=1).iloc[:10000, :]
    data2 = un_data.loc[(data["CRASHSEV"] == 2)].sample(frac=1).iloc[:10000, :]
    data3 = un_data.loc[(data["CRASHSEV"] == 3)].sample(frac=1).iloc[:10000, :]
    data4 = un_data.loc[(data["CRASHSEV"] == 4)].sample(frac=1).iloc[:10000, :]
    ba_data = pd.concat([data1,data2,data3,data4], axis=0).sample(frac=1).reset_index(drop=True)  #0是按行链接
    return ba_data
其中,sample()参数frac是要返回的比例,比如df中有10行数据,我只想返回其中的30%,那么frac=0.3


以下内容转载自:https://blog.csdn.net/liuweiyuxiang/article/details/78241530

今天我们就来谈一谈其强大的数据筛选功能,主要包括两大类,按照条件筛选和按照索引筛选。可以对行进行筛选,也可以按照列进行筛选。

条件筛选

首先为了方便说明问题,新建一个DataFrame


In [2]: df  #原文作者没有贴全创建df的代码 ,df结构如下
Out[2]:
    a   b   c
0   0   2   4
1   6   8  10
2  12  14  16
3  18  20  22
4  24  26  28
5  30  32  34
6  36  38  40
7  42  44  46
8  48  50  52
9  54  56  58

单条件筛选

  • 如果选取a列的取值大于30的记录可以这么写
In [3]: df[df['a']>30]
Out[3]:
    a   b   c
6  36  38  40
7  42  44  46
8  48  50  52
9  54  56  58
  • 如果想筛选a列的取值大于30的记录,但是之显示满足条件的b,c列的值可以这么写
In [5]: df[['b','c']][df['a']>30]
Out[5]:
    b   c
6  38  40
7  44  46
8  50  52
9  56  58
  • 使用isin函数根据特定值筛选记录。筛选a值等于30或者54的记录
In [10]: df[df.a.isin([30, 54])]
Out[10]:
    a   b   c
5  30  32  34
9  54  56  58

多条件筛选

可以使用&(并)与| (或)操作符或者特定的函数实现多条件筛选 

- 使用&筛选a列的取值大于30,b列的取值大于40的记录

In [8]: df[(df['a'] > 30) & (df['b'] > 40)]
Out[8]:
    a   b   c
7  42  44  46
8  48  50  52
9  54  56  58
  • 使用numpy的logical_and函数完成同样的功能
In [9]: df[np.logical_and(df['a']> 30,df['b']>40)]
Out[9]:
    a   b   c
7  42  44  46
8  48  50  52
9  54  56  58

排除特定行

筛选特定行做起来很方便,一可以使用特定的函数完成,但是排除含特定值的行就需要做一些变通了。例如,我们选出a列的值不等于30或者54的记录。基本的做法是将a列选择出来,把值30和54剔除,再使用isin函数。

In [11]: ex_list=list(df['a'])

In [13]: ex_list.remove(30)

In [14]: ex_list.remove(54)

In [15]: ex_list
Out[15]: [0, 6, 12, 18, 24, 36, 42, 48]
#使用切片操作选择特定的行
In [22]: df[1:4]
Out[22]:
    a   b   c
1   6   8  10
2  12  14  16
3  18  20  22
#传入列名选择特定的列
In [24]: df[['a','c']]
Out[24]:
    a   c
0   0   4
1   6  10
2  12  16
3  18  22
4  24  28
5  30  34
6  36  40
7  42  46
8  48  52
9  54  58
In [16]: df[df.a.isin(ex_list)]

索引筛选

切片操作

loc函数

当每列已有column name时,用 df [ ‘a’ ] 就能选取出一整列数据。如果你知道column names 和index(这里df的index没有指定,是默认生成的下标),且两者都很好输入,可以选择 .loc同时进行行列选择。

In [28]: df.loc[0,'c']
Out[28]: 4

In [29]: df.loc[1:4,['a','c']]
Out[29]:
    a   c
1   6  10
2  12  16
3  18  22
4  24  28

In [30]: df.loc[[1,3,5],['a','c']]
Out[30]:
    a   c
1   6  10
3  18  22
5  30  34

iloc

如果column name太长,输入不方便,或者index是一列时间序列,更不好输入,那就可以选择 .iloc了,该方法接受列名的index,iloc 使得我们可以对column使用slice(切片)的方法对数据进行选取。这边的 i 我觉得代表index,比较好记点。

In [35]: df.iloc[0,2]
Out[35]: 4

In [34]: df.iloc[1:4,[0,2]]
Out[34]:
    a   c
1   6  10
2  12  16
3  18  22

In [36]: df.iloc[[1,3,5],[0,2]]
Out[36]:
    a   c
1   6  10
3  18  22
5  30  34

In [38]: df.iloc[[1,3,5],0:2]
Out[38]:
    a   b
1   6   8
3  18  20
5  30  32

根据指定行index及列label,快速定位DataFrame的元素,选择列时仅支持列名。

In [46]: df.at[3,'a']
Out[46]: 18

iat函数

与at的功能相同,只使用索引参数。

In [49]: df.iat[3,0]
Out[49]: 18

In [50]: df.iat[3,'a']
ValueError: iAt based indexing can only have integer indexers

关于数据筛选函数的用法也可以参考python pandas dataframe 行列选择,切片操作


猜你喜欢

转载自blog.csdn.net/Dawei_01/article/details/80454922