4.5 Pandas中的Dataframe数据选取(一)(Python)

目录

前言

在初级python的开发中,感觉最难的还是pd数据结构的处理,趁项目不忙,把df相关的函数处理整理了一下。

一、df的创建

1. 初期数据定义

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']
}

2. 定义下标

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

3. df创建

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

完整代码:

# -*- 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中的数据行选取

在Dataframe中选取数据大抵包括3中情况:

    1)行(列)选取(单维度选取):df[]。这种情况一次只能选取行或者列,即一次选取中,只能为行或者列设置筛选条件(只能为一个维度设置筛选条件)。
    2)区域选取(多维选取):df.loc[],df.iloc[],df.ix[]。这种方式可以同时为多个维度设置筛选条件。
    3)单元格选取(点选取):df.at[],df.iat[]。准确定位一个单元格。
    

1. 初期数据定义

接上记:

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)

2. 行选取(单维度选取)

① 整数索引切片:前闭后开 [ , )
# 选取第一行df数据
df_line_1 = df[0:1]

print(df_line_1)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes

# 选取第三行df数据
df_line_3 = df[2:3]

print(df_line_3)

控制台输出结果:

         name  age weight is_single_dog
index2  NAME2    2    102            no

# 选取第二行到第三行df数据
df_line_2_to_3 = df[1:3]

print(df_line_2_to_3)

控制台输出结果:

         name  age weight is_single_dog
index1  NAME1    1    101           yes
index2  NAME2    2    102            no

② 标签索引切片:前闭后闭 [ , ]
# 选取第一行df数据
df_line_1 = df[:'index0']

print(df_line_1)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes

# 选取第三行df数据
df_line_3 = df['index2':'index2']

print(df_line_3)

控制台输出结果:

         name  age weight is_single_dog
index2  NAME2    2    102            no

# 选取第二行到第三行df数据
df_line_2_to_3 = df['index1':'index2']

print(df_line_2_to_3)

控制台输出结果:

         name  age weight is_single_dog
index1  NAME1    1    101           yes
index2  NAME2    2    102            no

③ 布尔数组
# 选取第一行df数据
df_line_1 = df[[True, False, False, False, False, False, False, False, False, False]]

print(df_line_1)

控制台输出结果:

         name  age   weight is_single_dog
index0  NAME0    0  weight0           yes

# 选取第三行df数据
df_line_3 = df[[False, False, True, False, False, False, False, False, False, False]]

print(df_line_3)

控制台输出结果:

         name  age weight is_single_dog
index2  NAME2    2    102            no

# 选取第二行到第三行df数据
df_line_2_to_3 = df[[False, True, True, False, False, False, False, False, False, False]]

print(df_line_2_to_3)

控制台输出结果:

         name  age weight is_single_dog
index1  NAME1    1    101           yes
index2  NAME2    2    102            no

④ 单条件选取
# 条件(age > 8)选取行df数据
df_line_age_over_8 = df[df['age'] > 8]

print(df_line_age_over_8)

控制台输出结果:

         name  age weight is_single_dog
index9  NAME9    9    109            no

⑤ 多条件选取

注意事项: 多条件选取数据时,单个条件最好用括号括起来,防止出错

"""
选取满足以下条件的df数据行:
 1. age > 5
 2. weight 不为空
 3. is_single_dog = no

"""

df_line_condition_more = df[(df['age'] > 5) & (df['weight'] is not None) & (df['is_single_dog'] == 'no')]

print(df_line_condition_more)

控制台输出结果:

         name  age weight is_single_dog
index6  NAME6    6    NaN            no
index8  NAME8    8    108            no
index9  NAME9    9    109            no

注意事项:NaN的处理之后再说

"""
选取满足以下任意条件的df数据行:
 1. name = NAME9
 2. age > 7 且 age < 9
 3. weight = 107

"""

df_line_condition_more = df[(df['name'] == 'NAME9') | ((df['age'] > 7) & (df['age'] < 9)) | (df['weight'] == 107)]

print(df_line_condition_more)

控制台输出结果:

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ibun.song

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

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

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

打赏作者

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

抵扣说明:

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

余额充值