2.Pandas 选择数据

0 引言

继续学习Pandas,一个表格实际处理时,会有各种各样表格中数据的选择,主要是对DataFrame的操作,比如按列表头选取,按行索引选取等等。

1 Pandas中DataFrame的数据选择

先导入Pandas和Numpy库

import pandas as pd
import numpy as np

创建一个DataFrame表格

dates = pd.date_range('20200101',periods=6)
df1 = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['A','B','C','D'])
df1
ABCD
2020-01-010123
2020-01-024567
2020-01-03891011
2020-01-0412131415
2020-01-0516171819
2020-01-0620212223

通过列表头来提取数据

df1['A']
2020-01-01     0
2020-01-02     4
2020-01-03     8
2020-01-04    12
2020-01-05    16
2020-01-06    20
Freq: D, Name: A, dtype: int32

也可以用 .A ,效果与前面一样

df1.A
2020-01-01     0
2020-01-02     4
2020-01-03     8
2020-01-04    12
2020-01-05    16
2020-01-06    20
Freq: D, Name: A, dtype: int32

获取指定行索引的表格数据

df1[0:2]
ABCD
2020-01-010123
2020-01-024567

通过行标签来获取指定数据

df1['20200102':'20200104']
ABCD
2020-01-024567
2020-01-03891011
2020-01-0412131415

通过标签提取指定表格数据

# 通过标签选择数据
df1.loc['20200102']
A    4
B    5
C    6
D    7
Name: 2020-01-02 00:00:00, dtype: int32

提取指定行和列数据

df1.loc['20200101',['A','B','C']]
A    0
B    1
C    2
Name: 2020-01-01 00:00:00, dtype: int32

:放在 loc 第一个参数位置,获取所有行

df1.loc[:,['A','B']]
AB
2020-01-0101
2020-01-0245
2020-01-0389
2020-01-041213
2020-01-051617
2020-01-062021

通过 .iloc 指定位置获取数据

#通过位置选择数据
df1.iloc[2]
A     8
B     9
C    10
D    11
Name: 2020-01-03 00:00:00, dtype: int32
df1.iloc[1:3,2:4] # 提取1到3行,2到4列
CD
2020-01-0267
2020-01-031011

混合标签获取数据

# 混合标签位置选择
df1.ix[2:4,['A','C']]
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
AC
2020-01-03810
2020-01-041214
df1.ix['20200102':'20200104',2:4]
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.
CD
2020-01-0267
2020-01-031011
2020-01-041415
df1.A
2020-01-01     0
2020-01-02     4
2020-01-03     8
2020-01-04    12
2020-01-05    16
2020-01-06    20
Freq: D, Name: A, dtype: int32

提取指定列并判断该列是否大于某个数,结果显示True或False

df1.A > 6
2020-01-01    False
2020-01-02    False
2020-01-03     True
2020-01-04     True
2020-01-05     True
2020-01-06     True
Freq: D, Name: A, dtype: bool
df1[df1.A > 6]
ABCD
2020-01-03891011
2020-01-0412131415
2020-01-0516171819
2020-01-0620212223
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZPILOTE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值