美文网首页
pandas dataframe中的数据选择3

pandas dataframe中的数据选择3

作者: 筝韵徽 | 来源:发表于2019-01-06 14:14 被阅读88次
import pandas as pd
import numpy as np

.iloc 选择定位数据

df = pd.read_csv('data/sample_data.csv',index_col=0)
df
image.png

iloc基于数值索引选择数据

选择索引为3的一行数据,对应的是行索引名称为Penelope的哪一行,返回的是Seires

df.iloc[3]
state        AL
color     white
food      Apple
age           4
height       80
score       3.3
Name: Penelope, dtype: object

上边的例子是返回Series,下边的列子是返回DataFrame形式,如下

 df.iloc[[3]]
image.png

选择多行

df.iloc[[5,3,2]]
image.png

切片选择多行

df.iloc[3:5]
image.png
df.iloc[2:]
image.png
df.iloc[1:4:2]
image.png

选择行列

  • 多行多列
df.iloc[[2,5],[0,3]]
image.png
  • 行切片,多列
df.iloc[1:5,[0,3,4]]
image.png
  • 行列切片选择
df.iloc[2:5,1:4]
image.png
  • 选择单元格
df.iloc[2,5]
9.0
  • 选择所有行,1列,以series返回
df.iloc[:,5]
Jane         4.6
Niko         8.3
Aaron        9.0
Penelope     3.3
Dean         1.8
Christina    9.5
Cornelia     2.2
Name: score, dtype: float64
  • 选择所有行,1列以DataFrame返回
df.iloc[:,[5]]
image.png
  • 选单行所有列,以series返回
df.iloc[1,:]
state        TX
color     green
food       Lamb
age           2
height       70
score       8.3
Name: Niko, dtype: object
  • 选择单行所有列,以DataFrame返回
df.iloc[[1],:]
image.png

说明,pandas有.ix方法,在使用python进行数据分析第一版中有介绍,但是该该方法已经过时,尽量不要再用

Series 中通用 .iloc .loc

s=df['food']
s
Jane          Steak
Niko           Lamb
Aaron         Mango
Penelope      Apple
Dean         Cheese
Christina     Melon
Cornelia      Beans
Name: food, dtype: object
type(s)
pandas.core.series.Series

loc 使用

s.loc['Dean']
'Cheese'
s.iloc[3]
'Apple'
s.loc[:]
Jane          Steak
Niko           Lamb
Aaron         Mango
Penelope      Apple
Dean         Cheese
Christina     Melon
Cornelia      Beans
Name: food, dtype: object
s.loc['Niko':'Dean']
Niko          Lamb
Aaron        Mango
Penelope     Apple
Dean        Cheese
Name: food, dtype: object
s.loc['Dean':]
Dean         Cheese
Christina     Melon
Cornelia      Beans
Name: food, dtype: object
s.loc[['Niko','Dean']]
Niko      Lamb
Dean    Cheese
Name: food, dtype: object

iloc 使用

s.iloc[2]
'Mango'
s.iloc[[2,3]]
Aaron       Mango
Penelope    Apple
Name: food, dtype: object
s.iloc[1:3]
Niko      Lamb
Aaron    Mango
Name: food, dtype: object
s.iloc[1:]
Niko           Lamb
Aaron         Mango
Penelope      Apple
Dean         Cheese
Christina     Melon
Cornelia      Beans
Name: food, dtype: object

相关文章

  • 文科生学Python系列9: Pandas入门

    第五课内容:Pandas入门 pandas DataFrame(数据框)pandas Series数据的选择案例:...

  • pandas dataframe中的数据选择3

    .iloc 选择定位数据 iloc基于数值索引选择数据 选择索引为3的一行数据,对应的是行索引名称为Penelop...

  • Pandas学习笔记

    pandas笔记 插入pandas 创建序列 创建dataframe 选择数据 设置值 处理丢失数据 导入导出数据...

  • Pandas

    Pandas 目录一、Pandas基础二、Pandas三大数据结构1.Series2.DataFrame3.Ind...

  • 03. Pandas数据结构

    [toc] 03. Pandas数据结构 Series DataFrame 从DataFrame中查询出Serie...

  • 2020-02-12

    Pandas笔记之创建 Pandas DataFrame DataFrame为Pandas的第二种主要数据结构,是...

  • pandas2

    3、Pandas 数据结构 - DataFrame DataFrame 是一个表格型的数据结构,它含有一组有序的列...

  • pandas dataframe中的数据选择1

    使用[], loc ,iloc 选择数据 先看看 DataFrame 组成有哪些 ,如下: DataFrame 有...

  • pandas dataframe中的数据选择4

    对比 上边的例子选择了行数字索引2--6的数据 上边的例子选择了行Jane--Aaron的数据 显然下边的用法更加...

  • pandas dataframe中的数据选择2

    使用.loc 选择数据 类似切片范围选择 行范围选择 使用.loc选择行,列数据.loc[rowindexes,c...

网友评论

      本文标题:pandas dataframe中的数据选择3

      本文链接:https://www.haomeiwen.com/subject/xzdsrqtx.html