本文由 伯乐在线 - 艾凌风 翻译,Daetalus 校稿。未经许可,禁止转载!
英文出处:pbpython。欢迎加入翻译组。
介绍
读者对于本系列第一篇文章的回应,让我感到很兴奋。感谢大家正面的反馈。我想把本系列继续下去,重点介绍其他的一些你经常使用Excel完成的任务,并且展示给你如何在pandas 中使用相同的功能。
在第一篇文章中,我着重介绍了Excel中常见的数学计算工作,以及在pandas如何完成这些工作。在本文中,我们将着重介绍一些常见的选择和筛选任务,并且介绍如何在pandas中完成同样的事情。
设置
如果您想要继续下去,您可以下载本excel文件。
导入pandas和numpy模块。
1
2
import
pandas as pd
import
numpy as np
导入我们样本公司销售年销售额的Excel文件。
1
df
=
pd.read_excel(
"sample-salesv3.xlsx"
)
快速浏览一下数据类型,以确保所以事情都能如预期一样运行。
1
2
3
4
5
6
7
8
account number int64
name
object
sku
object
quantity int64
unit price float64
ext price float64
date
object
dtype:
object
你会注意到,我们的date列,显示的是一个通用对象
。我们准备把它转换为日期对象,来简化将来会用到的一些选择操作。
1
2
df[
'date'
]
=
pd.to_datetime(df[
'date'
])
df.head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:511 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 10:00:472 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:583 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 15:05:224 | 412290Jerde-HilpertS2-34077683.21499.262014-01-01 23:26:551
2
3
4
5
6
7
8
account number int64
name
object
sku
object
quantity int64
unit price float64
ext price float64
date datetime64[ns]
dtype:
object
现在,data变成了一个datetime类型的对象,这对于将来的操作是很有用的。
筛选数据
我认为在Excel中最方便的功能是筛选。我想几乎每一次有人拿到一个任意大小的Excel文件,当他们想要筛选数据的时候,都会使用这个功能。
如图,对本数据集使用该功能:
同Excel中的筛选功能一样,你可以使用pandas来筛选和选择某个特定数据的子集。
比方说,如果我们仅仅想查看一个特定的账号,我们可以简单是在Excel中完成,或是使用pandas完成操作。
下面是Excel的筛选解决方案:
在pandas中执行相关操作比Excel中更加直观。注意,我将会使用head
函数来显示前面几个结果。这仅仅是为了让本文保持简短。
df[df[
"account number"
]
=
=
307599
].head()
你还可以以数值为基准来进行筛选。我就不再举任何Excel的例子了。我相信你能明白。
df[df[
"quantity"
] >
22
].head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:512 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:583 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 15:05:2214 | 737550Fritsch, Russel and AndersonB1-531022371.561645.882014-01-04 08:57:4815 | 239344Stokes LLCS1-065323471.512431.342014-01-04 11:34:58如果我们想要更多复杂的筛选,我们可以可以使用map
来以多重标准进行筛选。在这个例子中,从B1中查找以“sku”中起始的项目。
df[df[
"sku"
].
map
(
lambda
x: x.startswith(
'B1'
))].head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:512 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:586 | 218895Kulas IncB1-65551231.1062.202014-01-02 10:57:2314 | 737550Fritsch, Russel and AndersonB1-531022371.561645.882014-01-04 08:57:4817 | 239344Stokes LLCB1-508091416.23227.222014-01-04 22:14:32把两个或更多的语句连接起来很简单,用&就可以。
df[df[
"sku"
].
map
(
lambda
x: x.startswith(
'B1'
)) & (df[
"quantity"
] >
22
)].head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:512 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:5814 | 737550Fritsch, Russel and AndersonB1-531022371.561645.882014-01-04 08:57:4826 | 737550Fritsch, Russel and AndersonB1-536364242.061766.522014-01-08 00:02:1131 | 714466Trantow-BarrowsB1-330873219.56625.922014-01-09 10:16:32pandas支持的另外一个很有用的函数是isin
。它使得我们可以定义一个列表,里面包含我们所希望查找的值
在这个例子中,我们查找包含两个特定account number值的全部项目。
1
df[df[
"account number"
].isin([
714466
,
218895
])].head()
| account number | name | sku | quantity | unit price | ext price | date |
1 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 10:00:472 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:585 | 714466Trantow-BarrowsS2-778961787.631489.712014-01-02 10:07:156 | 218895Kulas IncB1-65551231.1062.202014-01-02 10:57:238 | 714466Trantow-BarrowsS1-509612284.091849.982014-01-03 11:29:02pandas支持的另外一个函数叫做query
,它使得我们可以有效的再数据集中选择数据。使用它需要安装numexpr ,所以请确保你在进行下面步骤前已经进行了安装。
如果你想要通过名字来得到一个消费者列表,你可以使用query来完成,和前面展示的python语法类似。
1
df.query(
'name == ["Kulas Inc","Barton LLC"]'
).head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150BartonLLCB1-200003986.693380.912014-01-01 07:21:512 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:586 | 218895Kulas IncB1-65551231.1062.202014-01-02 10:57:2333 | 218895Kulas IncS1-06532322.3667.082014-01-09 23:58:2736 | 218895Kulas IncS2-340771673.041168.642014-01-10 12:07:30这里只是做个简单的示例,query函数能做到的还不止这些。我在此展示这些函数的用法,以便当你有需要的时候,会意识到可以用它。
处理日期
使用pandas,你可以对日期进行更加复杂的筛选。在我们处理日期前,我建议你把日期栏进行一个排序,以便返回的结果如你所愿。
1
2
df
=
df.sort(
'date'
)
df.head()
| account number | name | sku | quantity | unit price | ext price | date | 0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:511 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 10:00:472 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:583 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 15:05:224 | 412290Jerde-HilpertS2-34077683.21499.262014-01-01 23:26:55在操作日期前,为您展示python的筛选语法。
1
df[df[
'date'
] >
=
'20140905'
].head()
| account number | name | sku | quantity | unit price | ext price | date | 1042 | 163416Purdy-KundeB1-388514198.694046.292014-09-05 01:52:321043 | 714466Trantow-BarrowsS1-30248137.1637.162014-09-05 06:17:191044 | 729833Koepp LtdS1-654814816.04769.922014-09-05 08:54:411045 | 729833Koepp LtdS2-11481626.50159.002014-09-05 16:33:151046 | 737550Fritsch, Russel and AndersonB1-33364476.44305.762014-09-06 08:59:08pandas的一个特别棒的特性是它能够理解日期,所以它允许我们进行部分筛选。如果我只想要查看最近几个月的日期数据,我可以这样做。
1
df[df[
'date'
] >
=
'2014-03'
].head()
| account number | name | sku | quantity | unit price | ext price | date |
242 | 163416Purdy-KundeS1-302481965.031235.572014-03-01 16:07:40243 | 527099Sanford and SonsS2-82423376.21228.632014-03-01 17:18:01244 | 527099Sanford and SonsB1-50809870.78566.242014-03-01 18:53:09245 | 737550Fritsch, Russel and AndersonB1-508092050.111002.202014-03-01 23:47:17246 | 688981Keeling LLCB1-86481-197.16-97.162014-03-02 01:46:44当然,你可以把筛选标准链接起来。
1
df[(df[
'date'
] >
=
'20140701'
) & (df[
'date'
] <
=
'20140715'
)].head()
| account number | name | sku | quantity | unit price | ext price | date |
778 | 737550Fritsch, Russel and AndersonS1-654813570.512467.852014-07-01 00:21:58779 | 218895Kulas IncS1-30248916.56149.042014-07-01 00:52:38780 | 163416Purdy-KundeS2-824234468.273003.882014-07-01 08:15:52781 | 672390Kuhn-GusikowskiB1-042024899.394770.722014-07-01 11:12:13782 | 642753Pollich LLCS2-23246151.2951.292014-07-02 04:02:39由于pandas可以理解日期列,所以可以将日期值设为不同的格式,都会得到正确的结果。
| account number | name | sku | quantity | unit price | ext price | date |
1168 | 307599Kassulke, Ondricka and MetzS2-23246688.90533.402014-10-08 06:19:501169 | 424914White-TrantowS2-103422558.541463.502014-10-08 07:31:401170 | 163416Purdy-KundeS1-277222234.41757.022014-10-08 09:01:181171 | 163416Purdy-KundeB1-33087779.29555.032014-10-08 15:39:131172 | 672390Kuhn-GusikowskiB1-388513094.642839.202014-10-09 00:22:331
df[df[
'date'
] >
=
'10-10-2014'
].head()
| account number | name | sku | quantity | unit price | ext price | date |
1174 | 257198Cronin, Oberbrunner and SpencerS2-340771312.24159.122014-10-10 02:59:061175 | 740150Barton LLCS1-654812853.001484.002014-10-10 15:08:531176 | 146832Kiehn-SpinkaS1-277221564.39965.852014-10-10 18:24:011177 | 257198Cronin, Oberbrunner and SpencerS2-16558335.34106.022014-10-11 01:48:131178 | 737550Fritsch, Russel and AndersonB1-536361056.95569.502014-10-11 10:25:53当操作时间序列数据时,如果你把数据进行转化,以日期作为索引,我们可以做一些变相的筛选。
使用set_index
来设置新的索引。
1
2
df2
=
df.set_index([
'date'
])
df2.head()
| account number | name | sku | quantity | unit price | ext price | date | | | | | | |
2014-01-01 07:21:51 | 740150Barton LLCB1-200003986.693380.912014-01-01 10:00:47 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 13:24:58 | 218895Kulas IncB1-699242390.702086.102014-01-01 15:05:22 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 23:26:55 | 412290Jerde-HilpertS2-34077683.21499.26你可以通过切分数据来获取一段区间。
1
df2[
"20140101"
:
"20140201"
].head()
| account number | name | sku | quantity | unit price | ext price | date | | | | | | |
2014-01-01 07:21:51 | 740150Barton LLCB1-200003986.693380.912014-01-01 10:00:47 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 13:24:58 | 218895Kulas IncB1-699242390.702086.102014-01-01 15:05:22 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 23:26:55 | 412290Jerde-HilpertS2-34077683.21499.26再一次的,我们可以使用不同的日期表示方法来避免模棱两可的日期命名惯例。
1
df2[
"2014-Jan-1"
:
"2014-Feb-1"
].head()
| account number | name | sku | quantity | unit price | ext price | date | | | | | | |
2014-01-01 07:21:51 | 740150Barton LLCB1-200003986.693380.912014-01-01 10:00:47 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 13:24:58 | 218895Kulas IncB1-699242390.702086.102014-01-01 15:05:22 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 23:26:55 | 412290Jerde-HilpertS2-34077683.21499.261
df2[
"2014-Jan-1"
:
"2014-Feb-1"
].tail()
| account number | name | sku | quantity | unit price | ext price | date | | | | | | |
2014-01-31 22:51:18 | 383080Will LLCB1-059144380.173447.312014-02-01 09:04:59 | 383080Will LLCB1-20000733.69235.832014-02-01 11:51:46 | 412290Jerde-HilpertS1-277221121.12232.322014-02-01 17:24:32 | 412290Jerde-HilpertB1-86481335.99107.972014-02-01 19:56:48 | 412290Jerde-HilpertB1-200002378.901814.70 | account number | name | sku | quantity | unit price | ext price | date | | | | | | |
2014-01-01 07:21:51 | 740150Barton LLCB1-200003986.693380.912014-01-01 10:00:47 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 13:24:58 | 218895Kulas IncB1-699242390.702086.102014-01-01 15:05:22 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 23:26:55 | 412290Jerde-HilpertS2-34077683.21499.26 | account number | name | sku | quantity | unit price | ext price | date | | | | | | |
2014-12-01 20:15:34 | 714466Trantow-BarrowsS1-82801377.97233.912014-12-02 20:00:04 | 146832Kiehn-SpinkaS2-232463757.812138.972014-12-03 04:43:53 | 218895Kulas IncS2-778963077.442323.202014-12-03 06:05:43 | 141962Herman LLCB1-531022026.12522.402014-12-03 14:17:34 | 642753Pollich LLCB1-536361971.211352.99正如你所见到的那样,在进行基于日期的排序或者筛选时,可以有很多选择。
额外的字符串方法
Pandas同样已经支持了矢量字符串方法。
如果我们想识别出sku栏中包含某一特定值的全部值。我们可以使用str.contains
。在这个例子中,我们已知sku总是以一种相同的方式表示,所以B1仅会出现在sku的前面。你需要理解你的数据来保证你能够得到你想要的结果。
1
df[df[
'sku'
].
str
.contains(
'B1'
)].head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:512 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:586 | 218895Kulas IncB1-65551231.1062.202014-01-02 10:57:2314 | 737550Fritsch, Russel and AndersonB1-531022371.561645.882014-01-04 08:57:4817 | 239344Stokes LLCB1-508091416.23227.222014-01-04 22:14:32我们可以把查询连接起来并且使用排序来控制数据的顺序。
df[(df[
'sku'
].
str
.contains(
'B1-531'
)) & (df[
'quantity'
]>
40
)].sort(columns
=
[
'quantity'
,
'name'
],ascending
=
[
0
,
1
])
| account number | name | sku | quantity | unit price | ext price | date |
684 | 642753Pollich LLCB1-531024626.071199.222014-06-08 19:33:33792 | 688981Keeling LLCB1-531024541.191853.552014-07-04 21:42:22176 | 383080Will LLCB1-531024589.224014.902014-02-11 04:14:091213 | 604255Halvorson, Crona and ChamplinB1-531024155.052257.052014-10-18 19:27:011215 | 307599Kassulke, Ondricka and MetzB1-531024193.703841.702014-10-18 23:25:101128 | 714466Trantow-BarrowsB1-531024155.682282.882014-09-27 10:42:481001 | 424914White-TrantowB1-531024181.253331.252014-08-26 11:44:30彩蛋任务
在Excel中,我发现我自己经常会尝试从一个冗长的列表中,得到一个包含不重复项的小列表。在Excel中这件事情需要分几步来完成,但是在Pandas中却非常简单。有一种方式是使用Excel中提供的高级筛选工具来完成。
在pandas中,我们对某列使用这个unique函数来获取这个列表。
1
2
3
4
5
6
7
8
array([u
'Barton LLC'
, u
'Trantow-Barrows'
, u
'Kulas Inc'
,
u
'Kassulke, Ondricka and Metz'
, u
'Jerde-Hilpert'
, u
'Koepp Ltd'
,
u
'Fritsch, Russel and Anderson'
, u
'Kiehn-Spinka'
, u
'Keeling LLC'
,
u
'Frami, Hills and Schmidt'
, u
'Stokes LLC'
, u
'Kuhn-Gusikowski'
,
u
'Herman LLC'
, u
'White-Trantow'
, u
'Sanford and Sons'
,
u
'Pollich LLC'
, u
'Will LLC'
, u
'Cronin, Oberbrunner and Spencer'
,
u
'Halvorson, Crona and Champlin'
, u
'Purdy-Kunde'
], dtype
=
object
)
If we wanted to include the account number, we could use drop_duplicates .
如果我们想要包含账户号,我们可以使用drop_duplicates
。
df.drop_duplicates(subset
=
[
"account number"
,
"name"
]).head()
| account number | name | sku | quantity | unit price | ext price | date |
0 | 740150Barton LLCB1-200003986.693380.912014-01-01 07:21:511 | 714466Trantow-BarrowsS2-77896-163.16-63.162014-01-01 10:00:472 | 218895Kulas IncB1-699242390.702086.102014-01-01 13:24:583 | 307599Kassulke, Ondricka and MetzS1-654814121.05863.052014-01-01 15:05:224 | 412290Jerde-HilpertS2-34077683.21499.262014-01-01 23:26:55很显然我们放入了的数据超过了我们的需要,得到了一些无用的信息,因此,使用ix
来仅仅选择第一第二列。
1
df.drop_duplicates(subset
=
[
"account number"
,
"name"
]).ix[:,[
0
,
1
]]
| account number | name |
0 | 740150Barton LLC1 | 714466Trantow-Barrows2 | 218895Kulas Inc3 | 307599Kassulke, Ondricka and Metz4 | 412290Jerde-Hilpert7 | 729833Koepp Ltd9 | 737550Fritsch, Russel and Anderson10 | 146832Kiehn-Spinka11 | 688981Keeling LLC12 | 786968Frami, Hills and Schmidt15 | 239344Stokes LLC16 | 672390Kuhn-Gusikowski18 | 141962Herman LLC20 | 424914White-Trantow21 | 527099Sanford and Sons30 | 642753Pollich LLC37 | 383080Will LLC51 | 257198Cronin, Oberbrunner and Spencer67 | 604255Halvorson, Crona and Champlin106 | 163416Purdy-Kunde我认为这个记住这个单独的命令比记忆Excel的各步操作更容易。
如果你想要查看我的笔记 请随意下载。
结论
在我发表了我的第一篇文章之后,Dave Proffer在Twitter上转发了我的文章并评论到“打破你#excel沉迷的一些好技巧”。我觉得这句话非常准确,它描述了在我们的生活中使用Excel是有多么的频繁。大多数的人只管伸手去用却从来没有意识到它的局限性。我希望这个系列的文章可以帮助大家认识到我们还有其他的替代工具,Python+Pandas是一个极其强大的组合。
转载:用Pandas完成Excel中常见的任务(2)
0 0