4.7 Pandas中的Dataframe数据选取(三)(Python)

目录

前言

略。。。

一、初期数据准备

1. 初期数据定义

# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

data = {
    'name': ['NAME0', 'NAME1', 'NAME2', 'NAME3', 'NAME4', 'NAME5', 'NAME6', 'NAME7', 'NAME8', 'NAME9'],

    'age': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],

    'weight': ["weight0", 101, 102, np.nan, np.nan, 105, np.nan, 107, 108, 109],

    'is_single_dog': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']
}

indexs = ['index0', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9']

df = pd.DataFrame(data, index=indexs)

print(df)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes
index1  NAME1    1      101           yes
index2  NAME2    2      102            no
index3  NAME3    3      NaN           yes
index4  NAME4    4      NaN            no
index5  NAME5    5      105            no
index6  NAME6    6      NaN            no
index7  NAME7    7      107           yes
index8  NAME8    8      108            no
index9  NAME9    9      109            no

二、Dataframe中的数据区域选取

说明:

区域选取可以从多个维度(行和列)对数据进行筛选,可以通过df.loc[],df.iloc[],df.ix[]三种方法实现。采用df.loc[],df.iloc[],df.ix[]这三种方法进行数据选取时,方括号内必须有两个参数,第一个参数是对行的筛选条件,第二个参数是对列的筛选条件,两个参数用逗号隔开。df.loc[],df.iloc[],df.ix[]的区别如下:

  df.loc[]只能使用标签索引,不能使用整数索引,通过便签索引切边进行筛选时,前闭后闭。

  df.iloc[]只能使用整数索引,不能使用标签索引,通过整数索引切边进行筛选时,前闭后开。;

  df.ix[]既可以使用标签索引,也可以使用整数索引。

1. df.loc[ ]

① df.loc[ ]行选取
# 选取索引为index2的行df数据
df_line_2 = df.loc['index2', :]

print(df_line_2)

控制台输出结果:

name             NAME2
age                  2
weight             102
is_single_dog       no
Name: index2, dtype: object

# 选取索引为index0, index1, index2的行df数据
df_line_1_to_3 = df.loc[['index0', 'index1', 'index2'], :]

print(df_line_1_to_3)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes
index1  NAME1    1      101           yes
index2  NAME2    2      102            no

# 选取索引为index0到index3的行df数据,前闭后闭
df_index_0_to_3 = df.loc['index0': 'index3', :]

print(df_index_0_to_3)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes
index1  NAME1    1      101           yes
index2  NAME2    2      102            no
index3  NAME3    3      NaN           yes

# 用布尔数组选取前3行df数据
df_line_1_to_3 = df.loc[[True, True, True, False, False, False, False, False, False, False], :]

print(df_line_1_to_3)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes
index1  NAME1    1      101           yes
index2  NAME2    2      102            no

# 单条件选取( age > 5 )的行df数据(方式一)
df_age_over_5 = df.loc[df['age'] > 5, :]

print(df_age_over_5)

# 单条件选取( age > 5 )的行df数据(方式二)
df_age_over_5 = df.loc[df.loc[:, 'age'] > 5, :]

print(df_age_over_5)
# 单条件选取( age > 5 )的行df数据(方式三)
df_age_over_5 = df.loc[df.loc[:, 'age'] > 5, :]

print(df_age_over_5)
控制台输出结果:

         name  age weight is_single_dog
index6  NAME6    6    NaN            no
index7  NAME7    7    107           yes
index8  NAME8    8    108            no
index9  NAME9    9    109            no

# 单条件选取( age > 5 )的行df数据(方式三)
df_age_over_5 = df.loc[df.loc[:, 'age'] > 5, :]

print(df_age_over_5)
# lambda表达式,单条件选取( age > 5 )的行df数据(方式四)
df_age_over_5 = df.loc[lambda df:df['age'] > 5, :]

print(df_age_over_5)
控制台输出结果:

         name  age weight is_single_dog
index6  NAME6    6    NaN            no
index7  NAME7    7    107           yes
index8  NAME8    8    108            no
index9  NAME9    9    109            no

➁ df.loc[ ]列选取
# 选取'name'列的df数据
df_column_1 = df.loc[:, 'name']

print(df_column_1)

控制台输出结果:

index0    NAME0
index1    NAME1
index2    NAME2
index3    NAME3
index4    NAME4
index5    NAME5
index6    NAME6
index7    NAME7
index8    NAME8
index9    NAME9
Name: name, dtype: object

# 选取'name'到'weight'列的df数据
df_column_1_to_3 = df.loc[:, 'name':'weight']

print(df_column_1_to_3)

控制台输出结果:

         name  age   weight
index0  NAME0    0  weight0
index1  NAME1    1      101
index2  NAME2    2      102
index3  NAME3    3      NaN
index4  NAME4    4      NaN
index5  NAME5    5      105
index6  NAME6    6      NaN
index7  NAME7    7      107
index8  NAME8    8      108
index9  NAME9    9      109

# 选取'age'和'is_single_dog'列的df数据
df_column_2_and_4 = df.loc[:, ['age', 'is_single_dog']]

print(df_column_2_and_4)

控制台输出结果:

        age is_single_dog
index0    0           yes
index1    1           yes
index2    2            no
index3    3           yes
index4    4            no
index5    5            no
index6    6            no
index7    7           yes
index8    8            no
index9    9            no

# 用布尔数组的方式选取前3列
df_column_1_to_3 = df.loc[:, [True, True, True, False]]

print(df_column_1_to_3)

控制台输出结果:

         name  age   weight
index0  NAME0    0  weight0
index1  NAME1    1      101
index2  NAME2    2      102
index3  NAME3    3      NaN
index4  NAME4    4      NaN
index5  NAME5    5      105
index6  NAME6    6      NaN
index7  NAME7    7      107
index8  NAME8    8      108
index9  NAME9    9      109

③ df.loc[ ]行和列同时选取
# 输出 age>5 的name,age和weight
df_column_age_over_5 = df.loc[df['age'] > 5, ['name', 'age', 'weight']]

print(df_column_age_over_5)

控制台输出结果:

         name  age weight
index6  NAME6    6    NaN
index7  NAME7    7    107
index8  NAME8    8    108
index9  NAME9    9    109

# 输出name = NAME0或者name = NAME9的name,age和weight
df_column_name = df.loc[(df['name'] == 'NAME0') | (df['name'] == 'NAME9'), 'name':'weight']

print(df_column_name)

控制台输出结果:

         name  age   weight
index0  NAME0    0  weight0
index9  NAME9    9      109

2. df.iloc[ ]

① df.iloc[ ]行选取
# 选取第2行df数据
df_line_2 = df.iloc[1, :]

print(df_line_2)

控制台输出结果:

name             NAME1
age                  1
weight             101
is_single_dog      yes
Name: index1, dtype: object

# 选取前3行df数据
df_line_1_to_3 = df.iloc[:3, :]

print(df_line_1_to_3)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes
index1  NAME1    1      101           yes
index2  NAME2    2      102            no

# 选取第2行,第4行,第6行的df数据
df_line_2_4_6 = df.iloc[[1, 3, 5], :]

print(df_line_2_4_6)

控制台输出结果:

         name  age weight is_single_dog
index1  NAME1    1    101           yes
index3  NAME3    3    NaN           yes
index5  NAME5    5    105            no

# 通过布尔数组选取前3行的df数据
df_line_1_to_3 = df.iloc[[True, True, True, False, False, False, False, False, False, False], :]

print(df_line_1_to_3)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes
index1  NAME1    1      101           yes
index2  NAME2    2      102            no

➁ df.iloc[ ]列选取
# 取第2列的df数据
df_column_2 = df.iloc[:, 1]

print(df_column_2)

控制台输出结果:

index0    0
index1    1
index2    2
index3    3
index4    4
index5    5
index6    6
index7    7
index8    8
index9    9
Name: age, dtype: int64

# 选取前3列的df数据, 前闭后开
df_column_1_to_3 = df.iloc[:, 0:3]

print(df_column_1_to_3)

控制台输出结果:

         name  age   weight
index0  NAME0    0  weight0
index1  NAME1    1      101
index2  NAME2    2      102
index3  NAME3    3      NaN
index4  NAME4    4      NaN
index5  NAME5    5      105
index6  NAME6    6      NaN
index7  NAME7    7      107
index8  NAME8    8      108
index9  NAME9    9      109

# 选取1,3,4列的df数据, 前闭后开
df_column_1_3_4 = df.iloc[:, [0, 2, 3]]

print(df_column_1_3_4)

控制台输出结果:

         name   weight is_single_dog
index0  NAME0  weight0           yes
index1  NAME1      101           yes
index2  NAME2      102            no
index3  NAME3      NaN           yes
index4  NAME4      NaN            no
index5  NAME5      105            no
index6  NAME6      NaN            no
index7  NAME7      107           yes
index8  NAME8      108            no
index9  NAME9      109            no

# 通过布尔数组选取前3列的df数据
df_column_1_to_3 = df.iloc[:, [True, True, True, False]]

print(df_column_1_to_3)

控制台输出结果:

         name  age   weight
index0  NAME0    0  weight0
index1  NAME1    1      101
index2  NAME2    2      102
index3  NAME3    3      NaN
index4  NAME4    4      NaN
index5  NAME5    5      105
index6  NAME6    6      NaN
index7  NAME7    7      107
index8  NAME8    8      108
index9  NAME9    9      109

③ df.iloc[ ]行和列同时选取
# 选取第2行的第1列,第3列,第4列
df_line2_column134 = df.iloc[1, [0, 2, 3]]

print(df_line2_column134)

控制台输出结果:

name             NAME1
weight             101
is_single_dog      yes
Name: index1, dtype: object

# 选取前3行前三列的df数据
df_line3_column3 = df.iloc[:3, :3]

print(df_line3_column3)

控制台输出结果:

         name  age   weight
index0  NAME0    0  weight0
index1  NAME1    1      101
index2  NAME2    2      102

三、Dataframe中的数据单元格选取

说明:

单元格选取包括df.at[]和df.iat[]两种方法。
df.at[]和df.iat[]使用时必须输入两个参数,即行索引和列索引,其中df.at[]只能使用标签索引,df.iat[]只能使用整数索引。
df.at[]和df.iat[]选取的都是单个单元格(单行单列),所以返回值都为基本数据类型。

1. df.at[ ]

① df.at[ ]单元格选取
# 选取第2行age列的df数据
df_line_2_age = df.at['index1', 'age']

print(df_line_2_age)

控制台输出结果:

1

2. df.iat[ ]

① df.iat[ ]单元格选取
# 选取第4行第3列的df数据
df_line4_column3 = df.iat[3, 2]

print(df_line4_column3)

控制台输出结果:

nan

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ibun.song

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

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

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

打赏作者

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

抵扣说明:

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

余额充值