pandas数据筛选和csv操作的实现方法

yipeiwu_com6年前Python基础

1. 数据筛选

 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

(1)单条件筛选

df[df['a']>30]
# 如果想筛选a列的取值大于30的记录,但是之显示满足条件的b,c列的值可以这么写
df[['b','c']][df['a']>30]
# 使用isin函数根据特定值筛选记录。筛选a值等于30或者54的记录
df[df.a.isin([30, 54])]

(2)多条件筛选

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

# 使用&筛选a列的取值大于30,b列的取值大于40的记录
df[(df['a'] > 30) & (df['b'] > 40)]

(3)索引筛选

a. 切片操作

df[行索引,列索引]或df[[列名1,列名2]]

#使用切片操作选择特定的行
df[1:4]
#传入列名选择特定的列
df[['a','c']]

b. loc函数

当每列已有column name时,用 df [ ‘a' ] 就能选取出一整列数据。如果你知道column names 和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

c. 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

d. ix函数

ix的功能更加强大,参数既可以是索引,也可以是名称,相当于,loc和iloc的合体。需要注意的是在使用的时候需要统一,在行选择时同时出现索引和名称, 同样在同行选择时同时出现索引和名称。

df.ix[1:3,['a','b']]
Out[41]:
 a b
1 6 8
2 12 14
3 18 20

In [42]: df.ix[[1,3,5],['a','b']]
Out[42]:
 a b
1 6 8
3 18 20
5 30 32

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

e. at函数

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

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

f. iat函数

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

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

2. csv操作

csv文件内容

Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier X,001-1001,2341,$500.00 ,1/20/14
Supplier X,001-1001,2341,$500.00 ,1/20/14
Supplier X,001-1001,5467,$750.00 ,1/20/14
Supplier X,001-1001,5467,$750.00 ,1/20/14
Supplier Y,50-9501,7009,$250.00 ,1/30/14
Supplier Y,50-9501,7009,$250.00 ,1/30/14
Supplier Y,50-9505,6650,$125.00 ,2002/3/14
Supplier Y,50-9505,6650,$125.00 ,2002/3/14
Supplier Z,920-4803,3321,$615.00 ,2002/3/14
Supplier Z,920-4804,3321,$615.00 ,2002/10/14
Supplier Z,920-4805,3321,$615.00 ,2/17/14
Supplier Z,920-4806,3321,$615.00 ,2/24/14

(1)csv文件读写

关于read_csv函数中的参数说明参考博客:/post/164445.htm

import pandas as pd

# 读写csv文件
df = pd.read_csv("supplier_data.csv")
df.to_csv("supplier_data_write.csv",index=None)

(2)筛选特定的行

#Supplier Nmae列中姓名包含'Z',或者Cost列中的值大于600
print(df[df["Supplier Name"].str.contains('Z')])
print(df[df['Cost'].str.strip('$').astype(float) > 600])
print(df.loc[(df["Supplier Name"].str.contains('Z'))|(df['Cost'].str.strip('$').astype(float) > 600.0),:])

#行中的值属于某个集合
li = [2341,6650]
print(df[df['Part Number'].isin(li)])
print(df.loc[df['Part Number'].astype(int).isin(li),:])

#行中的值匹配某个模式
print(df[df['Invoice Number'].str.startswith("001-")])

 (3)选取特定的列

#选取特定的列
#列索引值,打印1,3列
print(df.iloc[:,1:4:2])
#列标题打印
print(df.loc[:,["Invoice Number", "Part Number"]])
#选取连续的行
print(df.loc[1:4,:])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中set常用操作汇总

sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, sli...

Python 解码Base64 得到码流格式文本实例

我就废话不多说了,直接上代码吧! # coding:utf8 import base64 def BaseToFlow(): while True: str =...

python连接池实现示例程序

复制代码 代码如下:import socketimport Queueimport threading def worker():    while Tru...

python程序快速缩进多行代码方法总结

python程序快速缩进多行代码方法总结

该语言中缩进是其精髓,通过缩进可以表示函数、循环等程序结构的范围。有时写完程序后,发现所有程序需要放入函数def中,这时就需要对一整块程序同时进行缩进,还有其他一些情况,也会需要多行整体...

python生成器,可迭代对象,迭代器区别和联系

python生成器,可迭代对象,迭代器区别和联系

生成器,可迭代对象,迭代器之间究竟是什么关系? 用一幅图来概括: 1.生成器 定义生成器 方式一: //区别于列表生成式 gen = [x*x for x in range(...