The Realization Method of Index and Selection of Pandas Data

Time:2019-8-8

We choose the DataFrame, mostly at three levels: row, region, and cell.

The corresponding methods are as follows:
I. Rows, Columns – > df[]
II. Region – > df. LOC [], df. iloc [], df. IX []
3. Cells – > df. at [], df. IAT []

Let’s start with the following exercises:


import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(6,4), index=list('abcdef'), columns=list('ABCD'))

1. df[]:

One-dimensional
Line dimension:
Integer slice, label slice, <Boolean array>
Column dimension:
Label Index, Label List, Callable

df[:3]
df['a':'c']
DF [[True, True, True, False, False] # The first three lines (the length of a Boolean array equals the number of rows)
Rows with DF [df ['A'] > 0] A column value greater than 0
DF [(df ['A'] > 0) | (df ['B'] > 0)] A column value is greater than 0, or B column value is greater than 0 row
DF [(df ['A'] > 0) & (df ['C'] > 0)] A column value is greater than 0, and rows with column C greater than 0

df['A']
df[['A','B']]
df[lambda df: df.columns[0]] # Callable

2. df.loc[]

Two-dimensional, row after row
Line dimension:
Label Index, Label Slice, Label List, <Boolean Array>, Callable
Column dimension:
Label Index, Label Slice, Label List, <Boolean Array>, Callable

df.loc['a', :]
df.loc['a':'d', :]
df.loc[['a','b','c'], :]
Df.loc [[True, True, True, False, False],:] The first three lines (the length of a Boolean array equals the number of rows)
df.loc[df['A']>0, :]
df.loc[df.loc[:,'A']>0, :]
df.loc[df.iloc[:,0]>0, :]
df.loc[lambda _df: _df.A > 0, :]
df.loc[:, 'A']
df.loc[:, 'A':'C']
df.loc[:, ['A','B','C']]
Df.loc[:, [True, True, True, False] # The first three columns (the length of a Boolean array equals the number of rows)
Df.loc [:, df.loc ['a'] > 0] columns with rows greater than 0
Df.loc [:, df.iloc [0] > 0] columns with rows greater than 0
df.loc[:, lambda _df: ['A', 'B']]

df.A.loc[lambda s: s > 0]

3. df.iloc[]

Two-dimensional, row after row
Line dimension:
Integer Index, Integer Slice, Integer List, Boolean Array
Column dimension:
Integer Index, Integer Slice, Integer List, Boolean Array, Callable

df.iloc[3, :]
df.iloc[:3, :]
df.iloc[[0,2,4], :]
Df.iloc [[True, True, True, False, False],:] The first three lines (the length of a Boolean array equals the number of rows)
Df.iloc [df ['A']> 0,:] Why not? Can't figure it out!
df.iloc[df.loc[:,'A']>0, :] #×
df.iloc[df.iloc[:,0]>0, :] #×
df.iloc[lambda _df: [0, 1], :]
df.iloc[:, 1]
df.iloc[:, 0:3]
df.iloc[:, [0,1,2]]
Df.iloc[:, [True, True, True, False] # The first three columns (the length of a Boolean array equals the number of rows)
df.iloc[:, df.loc['a']>0] #×
df.iloc[:, df.iloc[0]>0] #×
df.iloc[:, lambda _df: [0, 1]]

4. df.ix[]

Two-dimensional, row after row
Line dimension:
Integer Index, Integer Slice, Integer List,
Label Index, Label Slice, Label List,
Boolean Array,
    Callable
Column dimension:
Integer Index, Integer Slice, Integer List,
Label Index, Label Slice, Label List,
Boolean Array,
    Callable


df.ix[0, :]
df.ix[0:3, :]
df.ix[[0,1,2], :]

df.ix['a', :]
df.ix['a':'d', :]
df.ix[['a','b','c'], :]

df.ix[:, 0]
df.ix[:, 0:3]
df.ix[:, [0,1,2]]

df.ix[:, 'A']
df.ix[:, 'A':'C']
df.ix[:, ['A','B','C']]

5. df.at[]

Accurate positioning cell
Line dimension:
Label Index
Column dimension:
Label Index


df.at['a', 'A']

6. df.iat[]

Accurate positioning cell

Line dimension:
Integer Index
Column dimension:
Integer Index


df.iat[0, 0]

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]