Select 23 pandas functions

Time:2022-5-26

Official account: youer hut
Author: Peter
Editor: Peter

Hello, I’m Peter~

Select 23 functions commonly used by pandas from 26 letters and introduce their usage to you. There are no corresponding functions for O, y and Z.

Select 23 pandas functions
import pandas as pd
import numpy as np

The following describes how to use each function. For more details, please go to the official website:https://pandas.pydata.org/docs/reference/general_functions.html

Assign function

df = pd.DataFrame({
    'temp_c': [17.0, 25.0]},
    index=['Portland', 'Berkeley'])
df

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>temp_c</th>
</tr>
</thead>
<tbody>
<tr>
<th>Portland</th>
<td>17.0</td>
</tr>
<tr>
<th>Berkeley</th>
<td>25.0</td>
</tr>
</tbody>
</table>

</div>

#Generate new fields

df.assign(temp_f=df['temp_c'] * 9 / 5 + 32)

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>temp_c</th>
<th>temp_f</th>
</tr>
</thead>
<tbody>
<tr>
<th>Portland</th>
<td>17.0</td>
<td>62.6</td>
</tr>
<tr>
<th>Berkeley</th>
<td>25.0</td>
<td>77.0</td>
</tr>
</tbody>
</table>

</div>

DF # the original dataframe does not change

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>temp_c</th>
</tr>
</thead>
<tbody>
<tr>
<th>Portland</th>
<td>17.0</td>
</tr>
<tr>
<th>Berkeley</th>
<td>25.0</td>
</tr>
</tbody>
</table>

</div>

df["temp_f1"] = df["temp_c"] * 9 / 5 + 32
df

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>temp_c</th>
<th>temp_f1</th>
</tr>
</thead>
<tbody>
<tr>
<th>Portland</th>
<td>17.0</td>
<td>62.6</td>
</tr>
<tr>
<th>Berkeley</th>
<td>25.0</td>
<td>77.0</td>
</tr>
</tbody>
</table>

</div>

df

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>temp_c</th>
<th>temp_f1</th>
</tr>
</thead>
<tbody>
<tr>
<th>Portland</th>
<td>17.0</td>
<td>62.6</td>
</tr>
<tr>
<th>Berkeley</th>
<td>25.0</td>
<td>77.0</td>
</tr>
</tbody>
</table>

</div>

Bool function

Returns the bool value of a single element in a single series or dataframe: true or false

pd.Series([True]).bool()
True
pd.Series([False]).bool()
False
pd.DataFrame({'col': [True]}).bool()
True
pd.DataFrame({'col': [False]}).bool()
False
## multiple elements cause an error

# pd.DataFrame({'col': [True,False]}).bool()
Select 23 pandas functions

Concat function

This function is used to represent the splicing of multiple dataframes, either horizontally or vertically.

df1 = pd.DataFrame({
    "sid":["s1","s2"],
    "name":["xiaoming","Mike"]})
df1

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Mike</td>
</tr>
</tbody>
</table>

</div>

df2 = pd.DataFrame({
    "sid":["s3","s4"],
    "name":["Tom","Peter"]})
df2

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s3</td>
<td>Tom</td>
</tr>
<tr>
<th>1</th>
<td>s4</td>
<td>Peter</td>
</tr>
</tbody>
</table>

</div>

df3 = pd.DataFrame({
    "Address": ["Beijing", "Shenzhen"],             
    "sex":["Male","Female"]})
df3

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>address</th>
<th>sex</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Beijing</td>
<td>Male</td>
</tr>
<tr>
<th>1</th>
<td>Shenzhen</td>
<td>Female</td>
</tr>
</tbody>
</table>

</div>

#Usage 1: longitudinal
pd.concat([df1,df2])

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Mike</td>
</tr>
<tr>
<th>0</th>
<td>s3</td>
<td>Tom</td>
</tr>
<tr>
<th>1</th>
<td>s4</td>
<td>Peter</td>
</tr>
</tbody>
</table>

</div>

#Usage 2: Landscape
pd.concat([df1,df3],axis=1)

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
<th>address</th>
<th>sex</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
<td>Beijing</td>
<td>Male</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Mike</td>
<td>Shenzhen</td>
<td>Female</td>
</tr>
</tbody>
</table>

</div>

Dropna function

Delete null value

df4 = pd.DataFrame({
    "sid":["s1","s2", np.nan],             
    "name":["xiaoming",np.nan, "Mike"]})
df4

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>NaN</td>
</tr>
<tr>
<th>2</th>
<td>NaN</td>
<td>Mike</td>
</tr>
</tbody>
</table>

</div>

df4.dropna()

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
</tbody>
</table>

</div>

df4.dropna(subset=["name"])

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>2</th>
<td>NaN</td>
<td>Mike</td>
</tr>
</tbody>
</table>

</div>

Expand function

Use of explosion function: convert wide table to long table

df5 = pd.DataFrame({
    "sid":["s1","s2"],       
    "Phones": [["Huawei", "Xiaomi", "Yijia"], ["Samsung", "apple"]]
                   })
df5

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>phones</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>[Huawei, Xiaomi, one plus]</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>[Samsung, apple]</td>
</tr>
</tbody>
</table>

</div>

df5.explode("phones")

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>phones</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>Huawei</td>
</tr>
<tr>
<th>0</th>
<td>s1</td>
<td>Millet</td>
</tr>
<tr>
<th>0</th>
<td>s1</td>
<td>One plus</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Samsung</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Apples</td>
</tr>
</tbody>
</table>

</div>

df5

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>phones</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>[Huawei, Xiaomi, one plus]</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>[Samsung, apple]</td>
</tr>
</tbody>
</table>

</div>

Fillna function

Fill in missing values

df4

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>NaN</td>
</tr>
<tr>
<th>2</th>
<td>NaN</td>
<td>Mike</td>
</tr>
</tbody>
</table>

</div>

df4.fillna({"sid":"s3","name":"Peter"})

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Peter</td>
</tr>
<tr>
<th>2</th>
<td>s3</td>
<td>Mike</td>
</tr>
</tbody>
</table>

</div>

Groupby function

Functions of the same group statistics

#Borrow this result
df6 = df5.explode("phones")
df6

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>phones</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>Huawei</td>
</tr>
<tr>
<th>0</th>
<td>s1</td>
<td>Millet</td>
</tr>
<tr>
<th>0</th>
<td>s1</td>
<td>One plus</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Samsung</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Apples</td>
</tr>
</tbody>
</table>

</div>

df6.groupby("sid")["phones"].count()
sid
s1    3
s2    2
Name: phones, dtype: int64

Head function

View the data of the first few rows. The default is the first five rows

df7 = pd.DataFrame({
    "sid":list(range(10)),                
    "name":list(range(80,100,2))})
df7

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>80</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>82</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>84</td>
</tr>
<tr>
<th>3</th>
<td>3</td>
<td>86</td>
</tr>
<tr>
<th>4</th>
<td>4</td>
<td>88</td>
</tr>
<tr>
<th>5</th>
<td>5</td>
<td>90</td>
</tr>
<tr>
<th>6</th>
<td>6</td>
<td>92</td>
</tr>
<tr>
<th>7</th>
<td>7</td>
<td>94</td>
</tr>
<tr>
<th>8</th>
<td>8</td>
<td>96</td>
</tr>
<tr>
<th>9</th>
<td>9</td>
<td>98</td>
</tr>
</tbody>
</table>

</div>

DF7. Head() # defaults to the first 5 lines

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>80</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>82</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>84</td>
</tr>
<tr>
<th>3</th>
<td>3</td>
<td>86</td>
</tr>
<tr>
<th>4</th>
<td>4</td>
<td>88</td>
</tr>
</tbody>
</table>

</div>

Df7.head (3) # specify the first 3 lines

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>80</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>82</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>84</td>
</tr>
</tbody>
</table>

</div>

Isnull function

Determine whether there are missing values, super common functions

df4

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>xiaoming</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>NaN</td>
</tr>
<tr>
<th>2</th>
<td>NaN</td>
<td>Mike</td>
</tr>
</tbody>
</table>

</div>

Df4.isnull() # true indicates missing

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>False</td>
<td>False</td>
</tr>
<tr>
<th>1</th>
<td>False</td>
<td>True</td>
</tr>
<tr>
<th>2</th>
<td>True</td>
<td>False</td>
</tr>
</tbody>
</table>

</div>

df4.isnull(). Sum() # sum of missing fields
sid     1
name    1
dtype: int64
df6.isnull(). Sum() # has no missing value
sid       0
phones    0
dtype: int64

Join function

For connecting different dataframes:

df7 = pd.DataFrame({
    'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
    'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
df7

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>A0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>A1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>A2</td>
</tr>
<tr>
<th>3</th>
<td>K3</td>
<td>A3</td>
</tr>
<tr>
<th>4</th>
<td>K4</td>
<td>A4</td>
</tr>
<tr>
<th>5</th>
<td>K5</td>
<td>A5</td>
</tr>
</tbody>
</table>

</div>

df8 = pd.DataFrame({
    'key': ['K0', 'K1', 'K2'],
    'B': ['B0', 'B1', 'B2']})
df8

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>B0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>B1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>B2</td>
</tr>
</tbody>
</table>

</div>

df7.join(df8,lsuffix="_df7",rsuffix="_df8")

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key_df7</th>
<th>A</th>
<th>key_df8</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>A0</td>
<td>K0</td>
<td>B0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>A1</td>
<td>K1</td>
<td>B1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>A2</td>
<td>K2</td>
<td>B2</td>
</tr>
<tr>
<th>3</th>
<td>K3</td>
<td>A3</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>4</th>
<td>K4</td>
<td>A4</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>5</th>
<td>K5</td>
<td>A5</td>
<td>NaN</td>
<td>NaN</td>
</tr>
</tbody>
</table>

</div>

Kurt function

Find the kurtosis value of the data

df9 = pd.DataFrame({
    "A":[12, 4, 5, 44, 1], 
    "B":[5, 2, 54, 3, 2], 
    "C":[20, 16, 7, 3, 8], 
    "D":[14, 3, 17, 2, 6]}) 
df9

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>12</td>
<td>5</td>
<td>20</td>
<td>14</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>2</td>
<td>16</td>
<td>3</td>
</tr>
<tr>
<th>2</th>
<td>5</td>
<td>54</td>
<td>7</td>
<td>17</td>
</tr>
<tr>
<th>3</th>
<td>44</td>
<td>3</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<th>4</th>
<td>1</td>
<td>2</td>
<td>8</td>
<td>6</td>
</tr>
</tbody>
</table>

</div>

df9.kurt()
A    3.936824
B    4.941512
C   -1.745717
D   -2.508808
dtype: float64

LOC function

LOC is the abbreviation of location, locating and searching data

df9

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>12</td>
<td>5</td>
<td>20</td>
<td>14</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>2</td>
<td>16</td>
<td>3</td>
</tr>
<tr>
<th>2</th>
<td>5</td>
<td>54</td>
<td>7</td>
<td>17</td>
</tr>
<tr>
<th>3</th>
<td>44</td>
<td>3</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<th>4</th>
<td>1</td>
<td>2</td>
<td>8</td>
<td>6</td>
</tr>
</tbody>
</table>

</div>

DF9. LOC [1,:] # the data of all columns in the first row
A     4
B     2
C    16
D     3
Name: 1, dtype: int64
DF9. LOC [1:3, "B"] # column B of rows 1 to 3
1     2
2    54
3     3
Name: B, dtype: int64

Merge function

It is also a data merging function, similar to join in SQL, with the most powerful function

df7

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>A0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>A1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>A2</td>
</tr>
<tr>
<th>3</th>
<td>K3</td>
<td>A3</td>
</tr>
<tr>
<th>4</th>
<td>K4</td>
<td>A4</td>
</tr>
<tr>
<th>5</th>
<td>K5</td>
<td>A5</td>
</tr>
</tbody>
</table>

</div>

df8

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>B0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>B1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>B2</td>
</tr>
</tbody>
</table>

</div>

pd. Merge (DF7, DF8) # default how parameter is inner

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>A0</td>
<td>B0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>A1</td>
<td>B1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>A2</td>
<td>B2</td>
</tr>
</tbody>
</table>

</div>

pd.merge(df7,df8,how="outer")  

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>K0</td>
<td>A0</td>
<td>B0</td>
</tr>
<tr>
<th>1</th>
<td>K1</td>
<td>A1</td>
<td>B1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>A2</td>
<td>B2</td>
</tr>
<tr>
<th>3</th>
<td>K3</td>
<td>A3</td>
<td>NaN</td>
</tr>
<tr>
<th>4</th>
<td>K4</td>
<td>A4</td>
<td>NaN</td>
</tr>
<tr>
<th>5</th>
<td>K5</td>
<td>A5</td>
<td>NaN</td>
</tr>
</tbody>
</table>

</div>

Nunique function

Unique value for statistics

df10 = pd.DataFrame({
    "sid":list("acbdefg"),
    "score":[9,8,9,7,8,9,3]
                    })
df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
</tr>
</tbody>
</table>

</div>

df10.nunique()
sid      7
score    4
dtype: int64

pct_ Change function

Calculate the ratio of the current period to the previous period

s = pd.Series([90, 91, 85])
s
0    90
1    91
2    85
dtype: int64
s.pct_change()
0         NaN
1    0.011111
2   -0.065934
dtype: float64
(91 - 90) / 90
0.011111111111111112
(85 - 91) / 91
-0.06593406593406594
#Compared with the previous two periods
s.pct_change(periods=2)
0         NaN
1         NaN
2   -0.055556
dtype: float64
#If there is a null value, use the fill method
s = pd.Series([90, 91, None, 85])
s
0    90.0
1    91.0
2     NaN
3    85.0
dtype: float64
s.pct_change(fill_method='ffill')
0         NaN
1    0.011111
2    0.000000
3   -0.065934
dtype: float64

Query function

Query values according to criteria

df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
</tr>
</tbody>
</table>

</div>

df10.query("score >= 8")

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
</tr>
</tbody>
</table>

</div>

Rank function

The ranking function is similar to the window function of SQL:

df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
</tr>
</tbody>
</table>

</div>

df10["rank_10"] = df10["score"].rank()
df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
<th>rank_10</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
<td>6.0</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
<td>3.5</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
<td>6.0</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
<td>2.0</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
<td>3.5</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
<td>6.0</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
<td>1.0</td>
</tr>
</tbody>
</table>

</div>

df10["rank_10_max"] = df10["score"].rank(method="max")
df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
<th>rank_10</th>
<th>rank_10_max</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
<td>3.5</td>
<td>4.0</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
<td>2.0</td>
<td>2.0</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
<td>3.5</td>
<td>4.0</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
<td>1.0</td>
<td>1.0</td>
</tr>
</tbody>
</table>

</div>

df10["rank_10_min"] = df10["score"].rank(method="min")
df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
<th>rank_10</th>
<th>rank_10_max</th>
<th>rank_10_min</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
<td>5.0</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
<td>3.5</td>
<td>4.0</td>
<td>3.0</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
<td>5.0</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
<td>2.0</td>
<td>2.0</td>
<td>2.0</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
<td>3.5</td>
<td>4.0</td>
<td>3.0</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
<td>5.0</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
<td>1.0</td>
<td>1.0</td>
<td>1.0</td>
</tr>
</tbody>
</table>

</div>

sort_ Values function

A function that sorts data

df9

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>12</td>
<td>5</td>
<td>20</td>
<td>14</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>2</td>
<td>16</td>
<td>3</td>
</tr>
<tr>
<th>2</th>
<td>5</td>
<td>54</td>
<td>7</td>
<td>17</td>
</tr>
<tr>
<th>3</th>
<td>44</td>
<td>3</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<th>4</th>
<td>1</td>
<td>2</td>
<td>8</td>
<td>6</td>
</tr>
</tbody>
</table>

</div>

df9.sort_ Values ("a") # default is ascending

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>4</th>
<td>1</td>
<td>2</td>
<td>8</td>
<td>6</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>2</td>
<td>16</td>
<td>3</td>
</tr>
<tr>
<th>2</th>
<td>5</td>
<td>54</td>
<td>7</td>
<td>17</td>
</tr>
<tr>
<th>0</th>
<td>12</td>
<td>5</td>
<td>20</td>
<td>14</td>
</tr>
<tr>
<th>3</th>
<td>44</td>
<td>3</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>

</div>

#First ascending according to B, if B is the same, then descending according to d

df9.sort_values(["B","D"], ascending=[True,False])

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>4</th>
<td>1</td>
<td>2</td>
<td>8</td>
<td>6</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>2</td>
<td>16</td>
<td>3</td>
</tr>
<tr>
<th>3</th>
<td>44</td>
<td>3</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<th>0</th>
<td>12</td>
<td>5</td>
<td>20</td>
<td>14</td>
</tr>
<tr>
<th>2</th>
<td>5</td>
<td>54</td>
<td>7</td>
<td>17</td>
</tr>
</tbody>
</table>

</div>

Tail function

View data at the end

df7.tail()

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>K1</td>
<td>A1</td>
</tr>
<tr>
<th>2</th>
<td>K2</td>
<td>A2</td>
</tr>
<tr>
<th>3</th>
<td>K3</td>
<td>A3</td>
</tr>
<tr>
<th>4</th>
<td>K4</td>
<td>A4</td>
</tr>
<tr>
<th>5</th>
<td>K5</td>
<td>A5</td>
</tr>
</tbody>
</table>

</div>

df7.tail(3)

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>key</th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<th>3</th>
<td>K3</td>
<td>A3</td>
</tr>
<tr>
<th>4</th>
<td>K4</td>
<td>A4</td>
</tr>
<tr>
<th>5</th>
<td>K5</td>
<td>A5</td>
</tr>
</tbody>
</table>

</div>

Unique function

Find unique elements for each field

df10

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>score</th>
<th>rank_10</th>
<th>rank_10_max</th>
<th>rank_10_min</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
<td>5.0</td>
</tr>
<tr>
<th>1</th>
<td>c</td>
<td>8</td>
<td>3.5</td>
<td>4.0</td>
<td>3.0</td>
</tr>
<tr>
<th>2</th>
<td>b</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
<td>5.0</td>
</tr>
<tr>
<th>3</th>
<td>d</td>
<td>7</td>
<td>2.0</td>
<td>2.0</td>
<td>2.0</td>
</tr>
<tr>
<th>4</th>
<td>e</td>
<td>8</td>
<td>3.5</td>
<td>4.0</td>
<td>3.0</td>
</tr>
<tr>
<th>5</th>
<td>f</td>
<td>9</td>
<td>6.0</td>
<td>7.0</td>
<td>5.0</td>
</tr>
<tr>
<th>6</th>
<td>g</td>
<td>3</td>
<td>1.0</td>
<td>1.0</td>
<td>1.0</td>
</tr>
</tbody>
</table>

</div>

df10["score"].unique()
array([9, 8, 7, 3])
df10["rank_10"].unique()
array([6. , 3.5, 2. , 1. ])

value_ Counts function

Used to count the number of each unique value in the field

df6

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>sid</th>
<th>phones</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>s1</td>
<td>Huawei</td>
</tr>
<tr>
<th>0</th>
<td>s1</td>
<td>Millet</td>
</tr>
<tr>
<th>0</th>
<td>s1</td>
<td>One plus</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Samsung</td>
</tr>
<tr>
<th>1</th>
<td>s2</td>
<td>Apples</td>
</tr>
</tbody>
</table>

</div>

df6["sid"].value_counts()
s1    3
s2    2
Name: sid, dtype: int64
df6["phones"].value_counts()
Huawei 1
Apple 1
Samsung 1
One plus one
Xiaomi 1
Name: phones, dtype: int64

Where function

Used to find data in series or dataframe that meets a certain condition

w = pd.Series(range(7))
w
0    0
1    1
2    2
3    3
4    4
5    5
6    6
dtype: int64
#Display meeting the conditions; If it is not satisfied, replace it with a null value
w.where(w>3)
0    NaN
1    NaN
2    NaN
3    NaN
4    4.0
5    5.0
6    6.0
dtype: float64
#If the conditions are not met, replace with 8
w.where(w > 1, 8)
0    8
1    8
2    2
3    3
4    4
5    5
6    6
dtype: int64

XS function

This function is used in multi-level index to obtain the value at the specified index, and uses a key parameter to select multiple indexes for specific levels of data.

d = {'num_legs': [4, 4, 2, 2],
     'num_wings': [0, 0, 2, 2],
     'class': ['mammal', 'mammal', 'mammal', 'bird'],
     'animal': ['cat', 'dog', 'bat', 'penguin'],
     'locomotion': ['walks', 'walks', 'flies', 'walks']}
#Generate data
df11 = pd.DataFrame(data=d)
#Reset index
df11 = df11.set_index(['class', 'animal', 'locomotion'])
df11

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th></th>
<th></th>
<th>num_legs</th>
<th>num_wings</th>
</tr>
<tr>
<th>class</th>
<th>animal</th>
<th>locomotion</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan=”3″ valign=”top”>mammal</th>
<th>cat</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>dog</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>bat</th>
<th>flies</th>
<td>2</td>
<td>2</td>
</tr>
<tr>
<th>bird</th>
<th>penguin</th>
<th>walks</th>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>

</div>

#Gets the value of the specified index
df11.xs('mammal')

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th></th>
<th>num_legs</th>
<th>num_wings</th>
</tr>
<tr>
<th>animal</th>
<th>locomotion</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>cat</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>dog</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>bat</th>
<th>flies</th>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>

</div>

#Specify values at multiple indexes
df11.xs(('mammal', 'dog'))
/Applications/downloads/anaconda/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:2881: PerformanceWarning: indexing past lexsort depth may impact performance.
  return runner(coro)

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>num_legs</th>
<th>num_wings</th>
</tr>
<tr>
<th>locomotion</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
</tbody>
</table>

</div>

#Gets the value of the specified index and level

df11.xs('cat', level=1)

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th></th>
<th>num_legs</th>
<th>num_wings</th>
</tr>
<tr>
<th>class</th>
<th>locomotion</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>mammal</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
</tbody>
</table>

</div>

df11

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th></th>
<th></th>
<th>num_legs</th>
<th>num_wings</th>
</tr>
<tr>
<th>class</th>
<th>animal</th>
<th>locomotion</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan=”3″ valign=”top”>mammal</th>
<th>cat</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>dog</th>
<th>walks</th>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>bat</th>
<th>flies</th>
<td>2</td>
<td>2</td>
</tr>
<tr>
<th>bird</th>
<th>penguin</th>
<th>walks</th>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>

</div>

#Gets the value of multiple indexes and levels
df11.xs(('bird', 'walks'),level=[0, 'locomotion'])

<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

<table border=”1″>
<thead>
<tr style=”text-align: right;”>
<th></th>
<th>num_legs</th>
<th>num_wings</th>
</tr>
<tr>
<th>animal</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>penguin</th>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>

</div>

#Gets the value on the specified column and axis
df11.xs('num_wings', axis=1)
class   animal   locomotion
mammal  cat      walks         0
        dog      walks         0
        bat      flies         2
bird    penguin  walks         2
Name: num_wings, dtype: int64

Recommended Today

CI / CD process implementation and environment deployment (Theory)

Kunetes supports multiple running environments:Docker、 containerd、CRI-OAnd any implementationKubernetes CRI (container running environment interface)Kubernetes officially announced that it would give up its support for docker from v1.20. At present, the docker support function in kubelet has been deprecated and will be deleted in later versions. Gradually transfer to containerd. However, this article is still based on […]