Numpy & Pandas (莫烦 Python 数据处理教程)-Pandas学习笔记(2)-Pandas选择数据

Pandas学习笔记(1)-Pandas的基本介绍

教程视频为B站莫烦 Python 数据处理教程 第11课程

导入模块

import pandas as pd
import numpy as np

数据选择

dates = pd.date_range('20130101',periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['A','B','C','D'])
print(df)
#out:
             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
print(df['A'])
#out:
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
print(df['A'])
#out:
Freq: D, Name: A, dtype: int32 2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
print(df[0:3])   
#out:
Freq: D, Name: A, dtype: int32
            A  B   C   D
2013-01-01  0  1   2   3
2013-01-02  4  5   6   7
2013-01-03  8  9  10  11

select by label:loc 按标签筛选

print(df.loc['20130102'])   #按标签展示
#out:
A    4
B    5
C    6
D    7
Name: 2013-01-02 00:00:00, dtype: int32
print(df.loc[:,['A','B']])       #选择列的消息
#out:
             A   B
2013-01-01   0   1
2013-01-02   4   5
2013-01-03   8   9
2013-01-04  12  13
2013-01-05  16  17
2013-01-06  20  21
print(df.loc['20130102',['A','B']])       #输出特定位置的信息
#out:
A    4
B    5
Name: 2013-01-02 00:00:00, dtype: int32

通过位置选择 iloc

print(df.iloc[3,1])     #第三行第一列
#out:
13
print(df.iloc[3:5,1:3])
#out:
             B   C
2013-01-04  13  14
2013-01-05  17  18

Boolean indexing 条件筛选

print(df[df.A > 8])   #筛选A列中大于8的
#out:
             A   B   C   D
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值