切片、索引、操作和清理Pandas数据框架
在Pandas的帮助下,我们可以在数据集上执行许多功能,如切片、索引、操作和清理数据帧。
案例1:使用DataFrame.iloc[]对Pandas数据框架进行切片。
例子1:切分行数
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000],
['A.B.D Villers', 38, 74, 3428000],
['V.Kholi', 31, 70, 8428000],
['S.Smith', 34, 80, 4428000],
['C.Gayle', 40, 100, 4528000],
['J.Root', 33, 72, 7028000],
['K.Peterson', 42, 85, 2528000]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=['Name', 'Age', 'Weight', 'Salary'])
# data frame before slicing
df
输出:
# Slicing rows in data frame
df1 = df.iloc[0:4]
# data frame after slicing
df1
输出:
在上面的例子中,我们将数据框中的行切成片状。
例子2 :分割列
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000],
['A.B.D Villers', 38, 74, 3428000],
['V.Kholi', 31, 70, 8428000],
['S.Smith', 34, 80, 4428000],
['C.Gayle', 40, 100, 4528000],
['J.Root', 33, 72, 7028000],
['K.Peterson', 42, 85, 2528000]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=['Name', 'Age', 'Weight', 'Salary'])
# data frame before slicing
df
输出:
# Slicing columnss in data frame
df1 = df.iloc[:,0:2]
# data frame after slicing
df1
输出:
在上面的例子中,我们把数据框架中的列切开。
案例2:对Pandas数据框架进行索引
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000],
['A.B.D Villers', 38, 74, 3428000],
['V.Kholi', 31, 70, 8428000],
['S.Smith', 34, 80, 4428000],
['C.Gayle', 40, 100, 4528000],
['J.Root', 33, 72, 7028000],
['K.Peterson', 42, 85, 2528000]]
# creating a pandas dataframe and indexing it using Aplhabets
df = pd.DataFrame(player_list, columns=['Name', 'Age', 'Weight', 'Salary'],
index=['A', 'B', 'C', 'D', 'E', 'F', 'G'])
# Displaying data frame
df
输出:
在上面的例子中,我们对数据框架进行了索引。
案例3:操纵Pandas数据框架
对数据框架的操作可以通过多种方式进行,如应用函数、改变列的数据类型、拆分、向数据框架添加行和列等。
示例1:使用Dataframe.assign()将lambda函数应用于一个列。
# importing pandas library
import pandas as pd
# creating and initializing a list
values = [['Rohan', 455], ['Elvish', 250], ['Deepak', 495],
['Sai', 400], ['Radha', 350], ['Vansh', 450]]
# creating a pandas dataframe
df = pd.DataFrame(values, columns=['Name', 'Univ_Marks'])
# Applying lambda function to find percentage of
# 'Univ_Marks' column using df.assign()
df = df.assign(Percentage=lambda x: (x['Univ_Marks'] / 500 * 100))
# displaying the data frame
df
输出:
在上面的例子中,lambda函数被应用于’Univ_Marks’列,并在它的帮助下形成一个新的列’Percentity’。
示例2:以升序对数据框进行排序。
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000],
['A.B.D Villers', 38, 74, 3428000],
['V.Kholi', 31, 70, 8428000],
['S.Smith', 34, 80, 4428000],
['C.Gayle', 40, 100, 4528000],
['J.Root', 33, 72, 7028000],
['K.Peterson', 42, 85, 2528000]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=['Name', 'Age', 'Weight', 'Salary'])
# Sorting by column 'Weight'
df.sort_values(by=['Weight'])
输出:
在上面的例子中,我们按列 “重量 “对数据框进行排序。
案例4:清理Pandas数据框架
# importing pandas and Numpy libraries
import pandas as pd
import numpy as np
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000],
['A.B.D Villers', np.nan, 74, np.nan],
['V.Kholi', 31, 70, 8428000],
['S.Smith', 34, 80, 4428000],
['C.Gayle', np.nan, 100, np.nan],
[np.nan, 33, np.nan, 7028000],
['K.Peterson', 42, 85, 2528000]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=['Name', 'Age', 'Weight', 'Salary'])
df
输出:
# Checking for missing values
df.isnull().sum()
输出:
# dropping or cleaning the missing data
df= df.dropna()
df
输出:
在上面的例子中,我们清理了数据集中所有的缺失值。