欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?pandas的八個(gè)生命周期總結(jié)

 更新時(shí)間:2022年10月21日 08:22:01   作者:Python集中營(yíng)  
這篇文章主要從八個(gè)pandas的數(shù)據(jù)處理生命周期,整理匯總出pandas框架在整個(gè)數(shù)據(jù)處理過(guò)程中都是如何處理數(shù)據(jù)的,感興趣的小伙伴可以了解一下

這里從八個(gè)pandas的數(shù)據(jù)處理生命周期,整理匯總出pandas框架在整個(gè)數(shù)據(jù)處理過(guò)程中都是如何處理數(shù)據(jù)的。

也就是從pandas的數(shù)據(jù)表對(duì)象以及數(shù)據(jù)匯總、數(shù)據(jù)統(tǒng)計(jì)等等直到數(shù)據(jù)導(dǎo)出的八個(gè)處理過(guò)程來(lái)完成pandas使用的匯總處理。

首先,需要準(zhǔn)備好將python非標(biāo)準(zhǔn)庫(kù)導(dǎo)入進(jìn)來(lái),除了pandas之外一般伴隨數(shù)據(jù)分析處理使用的還有numpy科學(xué)計(jì)算庫(kù)。

# Importing the pandas library and giving it the alias pd.
import pandas as pd

# Importing the numpy library and giving it the alias np.
import numpy as np

1、數(shù)據(jù)表對(duì)象(DataFrame)

在pandas的數(shù)據(jù)分析處理中,主要依賴的是對(duì)DataFrame對(duì)象的處理來(lái)完成數(shù)據(jù)的提取、匯總、統(tǒng)計(jì)等操作。

那么在初始化DataFrame對(duì)象的時(shí)候有兩種方式,一種是直接讀取Excel、csv文件獲取數(shù)據(jù)后返回DataFrame數(shù)據(jù)對(duì)象。

# Reading the csv file and converting it into a dataframe.
dataframe_csv = pd.DataFrame(pd.read_csv('./data.csv'))

# Reading the excel file and converting it into a dataframe.
dataframe_xlsx = pd.DataFrame(pd.read_excel('./data.xlsx'))

另一種則是需要自己創(chuàng)建DataFrame對(duì)象的數(shù)據(jù),將字典等類(lèi)型的python對(duì)象直接初始化為DataFrame數(shù)據(jù)表的形式。

# Creating a dataframe with two columns, one called `name` and the other called `age`.
dataframe = pd.DataFrame({"編程語(yǔ)言": ['Java', 'Python', 'C++'],
                          "已誕生多少年": [23, 20, 28]},
                         columns=['編程語(yǔ)言', '已誕生多少年'])

2、數(shù)據(jù)表(DataFrame)結(jié)構(gòu)信息

通過(guò)DataFrame對(duì)象內(nèi)置的各種函數(shù)來(lái)查看數(shù)據(jù)維度、列名稱、數(shù)據(jù)格式等信息。

# Creating a dataframe with two columns, one called `name` and the other called `age`.
dataframe = pd.DataFrame({"編程語(yǔ)言": ['Java', 'Python', 'C++'],
                          "已誕生多少年": [23, 20, 28]},
                         columns=['編程語(yǔ)言', '已誕生多少年'])

【加粗】dataframe.info()

查看數(shù)據(jù)表的基本信息展示,包括列數(shù)、數(shù)據(jù)格式、列名稱、占用空間等。

dataframe.info()

# <class 'pandas.core.frame.DataFrame'>
# Index: 0 entries
# Data columns (total 2 columns):
#  #   Column  Non-Null Count  Dtype
# ---  ------  --------------  -----
#  0   編程語(yǔ)言    0 non-null      object
#  1   已誕生多少年  0 non-null      object
# dtypes: object(2)
# memory usage: 0.0+ bytes

【加粗】dataframe.columns

查看DataFrame對(duì)象的所有列的名稱,并返回?cái)?shù)組信息。

print('顯示所有列的名稱是:{0}'.format(dataframe.columns))

# 顯示所有列的名稱是:Index(['編程語(yǔ)言', '已誕生多少年'], dtype='object')

【加粗】dataframe['列名'].dtype

查看DataFrame對(duì)象中某一列的格式dtype是什么。

print('列名(編程語(yǔ)言)的格式是:{0}'.format(dataframe[u'編程語(yǔ)言'].dtype))

# 列名(編程語(yǔ)言)的格式是:object

【加粗】dataframe.shape

通過(guò)DataFrame對(duì)象的shape函數(shù),進(jìn)而展示出數(shù)據(jù)是幾行幾列的結(jié)構(gòu)。

print('dataframe的結(jié)構(gòu)是:{0}'.format(dataframe.shape))

# dataframe的結(jié)構(gòu)是:(3, 2)

【加粗】dataframe.values

使用DataFrame對(duì)象的values函數(shù),得出所有數(shù)據(jù)內(nèi)容的結(jié)果。

# Importing the pprint function from the pprint module.
from pprint import pprint

pprint('dataframe對(duì)象的值是:{0}'.format(dataframe.values))

# "dataframe對(duì)象的值是:[['Java' 23]\n ['Python' 20]\n ['C++' 28]]"

3、數(shù)據(jù)清洗

數(shù)據(jù)清洗即是對(duì)DataFrame對(duì)象中的數(shù)據(jù)進(jìn)行規(guī)范化的處理,比如空值的數(shù)據(jù)填充、重復(fù)數(shù)據(jù)的清理、數(shù)據(jù)格式的統(tǒng)一轉(zhuǎn)換等等。

【加粗】dataframe.fillna()

# 將所有數(shù)據(jù)為空的項(xiàng)填充為0
dataframe.fillna(value=0)

# 使用均值進(jìn)行填充
dataframe[u'已誕生多少年'].fillna(dataframe[u'已誕生多少年'].mean())

【加粗】map(str.strip)

# 去除指定列的首尾多余的空格后,再重新賦值給所在列

dataframe[u'編程語(yǔ)言'] = dataframe[u'編程語(yǔ)言'].map(str.strip)

【加粗】dataframe.astype

# 更改DataFrame數(shù)據(jù)對(duì)象中某個(gè)列的數(shù)據(jù)格式。

dataframe[u'已誕生多少年'].astype('int')

【加粗】dataframe.rename

# 更改DataFrame數(shù)據(jù)對(duì)象中某個(gè)列的名稱

dataframe.rename(columns={u'已誕生多少年': u'語(yǔ)言年齡'})

【加粗】 dataframe.drop_duplicates

# 以DataFrame中的某個(gè)列為準(zhǔn),刪除其中的重復(fù)項(xiàng)

dataframe[u'編程語(yǔ)言'].drop_duplicates()

【加粗】dataframe.replace

# 替換DataFrame數(shù)據(jù)對(duì)象中某個(gè)列中指定的值

dataframe[u'編程語(yǔ)言'].replace('Java', 'C#')

4、數(shù)據(jù)預(yù)梳理

數(shù)據(jù)預(yù)處理(data preprocessing)是指在主要的處理以前對(duì)數(shù)據(jù)進(jìn)行的一些處理。

如對(duì)大部分地球物理面積性觀測(cè)數(shù)據(jù)在進(jìn)行轉(zhuǎn)換或增強(qiáng)處理之前,首先將不規(guī)則分布的測(cè)網(wǎng)經(jīng)過(guò)插值轉(zhuǎn)換為規(guī)則網(wǎng)的處理,以利于計(jì)算機(jī)的運(yùn)算。

【加粗】數(shù)據(jù)合并

使用DataFrame對(duì)象數(shù)據(jù)合并的有四種方式可以選擇,分別是merge、append、join、concat方式,不同方式實(shí)現(xiàn)的效果是不同的。

接下來(lái)使用兩種比較常見(jiàn)的方式append、concat、join來(lái)演示一下DataFrame對(duì)象合并的效果。

使用兩個(gè)DataFrame的數(shù)據(jù)對(duì)象通過(guò)append將對(duì)象的數(shù)據(jù)內(nèi)容進(jìn)行合并。

# Creating a dataframe with two columns, one called `編程語(yǔ)言` and the other called `已誕生多少年`.
dataframeA = pd.DataFrame({"編程語(yǔ)言": ['Java', 'Python', 'C++'],
                           "已誕生多少年": [23, 20, 28]}, columns=['編程語(yǔ)言', '已誕生多少年'])

# Creating a dataframe with two columns, one called `編程語(yǔ)言` and the other called `已誕生多少年`.
dataframeB = pd.DataFrame({"編程語(yǔ)言": ['Scala', 'C#', 'Go'],
                           "已誕生多少年": [23, 20, 28]}, columns=['編程語(yǔ)言', '已誕生多少年'])

# Appending the dataframeB to the dataframeA.
res = dataframeA.append(dataframeB)

# Printing the result of the append operation.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 0    Java      23
# 1  Python      20
# 2     C++      28
# 0   Scala      23
# 1      C#      20
# 2      Go      28
#
# Process finished with exit code 0

使用兩個(gè)DataFrame的數(shù)據(jù)對(duì)象通過(guò)concat將對(duì)象的數(shù)據(jù)內(nèi)容進(jìn)行合并。

# Concatenating the two dataframes together.
res = pd.concat([dataframeA, dataframeB])

# Printing the result of the append operation.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 0    Java      23
# 1  Python      20
# 2     C++      28
# 0   Scala      23
# 1      C#      20
# 2      Go      28

concat函數(shù)的合并效果和append函數(shù)有異曲同工之妙,兩者同樣都是對(duì)數(shù)據(jù)內(nèi)容進(jìn)行縱向合并的。

使用兩個(gè)DataFrame的數(shù)據(jù)對(duì)象通過(guò)join將對(duì)象的數(shù)據(jù)結(jié)構(gòu)及數(shù)據(jù)內(nèi)容進(jìn)行橫向合并。

# Creating a dataframe with two columns, one called `編程語(yǔ)言` and the other called `已誕生多少年`.
dataframeC = pd.DataFrame({"編程語(yǔ)言": ['Java', 'Python', 'C++'],
                           "已誕生多少年": [23, 20, 28]}, columns=['編程語(yǔ)言', '已誕生多少年'])

# Creating a dataframe with one column called `歷史表現(xiàn)` and three rows.
dataframeD = pd.DataFrame({"歷史表現(xiàn)": ['A', 'A', 'A']})

# Joining the two dataframes together.
res = dataframeC.join(dataframeD, on=None)

# Printing the result of the append operation.
print(res)

#      編程語(yǔ)言  已誕生多少年 歷史表現(xiàn)
# 0    Java      23    A
# 1  Python      20    A
# 2     C++      28    A

可以發(fā)現(xiàn)使用join的函數(shù)之后,將dataframeD作為一個(gè)列擴(kuò)展了并且對(duì)應(yīng)的每一行都準(zhǔn)確的填充了數(shù)據(jù)A。

【加粗】設(shè)置索引

給DataFrame對(duì)象設(shè)置索引的話就比較方便了,直接DataFrame對(duì)象提供的set_index函數(shù)設(shè)置需要定義索引的列名稱就OK了。

# Creating a dataframe with two columns, one called `編程語(yǔ)言` and the other called `已誕生多少年`.
dataframeE = pd.DataFrame({"編程語(yǔ)言": ['Java', 'Python', 'C++'],
                           "已誕生多少年": [23, 20, 28]}, columns=['編程語(yǔ)言', '已誕生多少年'])

# Setting the index of the dataframe to the column `編程語(yǔ)言`.
dataframeE.set_index(u'編程語(yǔ)言')

# Printing the dataframeE.
print(dataframeE)

#      編程語(yǔ)言  已誕生多少年
# 0    Java      23
# 1  Python      20
# 2     C++      28

【加粗】數(shù)據(jù)排序

DataFrame數(shù)據(jù)對(duì)象的排序主要是通過(guò)索引排序、某個(gè)指定列排序的方式為參照完成對(duì)DataFrame對(duì)象中的整個(gè)數(shù)據(jù)內(nèi)容排序。

# Sorting the dataframeE by the index.
res = dataframeE.sort_index()

# Printing the res.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 0    Java      23
# 1  Python      20
# 2     C++      28

# Sorting the dataframeE by the column `已誕生多少年`.
res = dataframeE.sort_values(by=['已誕生多少年'], ascending=False)

# Printing the res.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 2     C++      28
# 0    Java      23
# 1  Python      20

sort_index函數(shù)是指按照當(dāng)前DataFrame數(shù)據(jù)對(duì)象的索引進(jìn)行排序,sort_values則是按照指定的一個(gè)或多個(gè)列的值進(jìn)行降序或者升序。

【加粗】數(shù)據(jù)分組

數(shù)據(jù)預(yù)處理中的數(shù)據(jù)分組主要是需要的分組的數(shù)據(jù)打上特殊的標(biāo)記以便于后期對(duì)數(shù)據(jù)的歸類(lèi)處理。

比較簡(jiǎn)單一些的分組處理可以使用numpy中提供的函數(shù)進(jìn)行處理,這里使用numpy的where函數(shù)來(lái)設(shè)置過(guò)濾條件。

# Creating a new column called `分組標(biāo)記(高齡/低齡)` and setting the value to `高` if the value in the column `已誕生多少年` is greater
# than or equal to 23, otherwise it is setting the value to `低`.
dataframeE['分組標(biāo)記(高齡/低齡)'] = np.where(dataframeE[u'已誕生多少年'] >= 23, '高', '低')

# Printing the dataframeE.
print(dataframeE)

#      編程語(yǔ)言  已誕生多少年 分組標(biāo)記(高齡/低齡)
# 0    Java      23           高
# 1  Python      20           低
# 2     C++      28           高

稍微復(fù)雜一些的過(guò)濾條件可以使用多條件的過(guò)濾方式找出符合要求的數(shù)據(jù)項(xiàng)進(jìn)行分組標(biāo)記。

# Creating a new column called `分組標(biāo)記(高齡/低齡,是否是Java)` and setting the value to `高/是` if the value in the column `已誕生多少年` is
# greater than or equal to 23 and the value in the column `編程語(yǔ)言` is equal to `Java`, otherwise it is setting the value to
# `低/否`.
dataframeE['分組標(biāo)記(高齡/低齡,是否是Java)'] = np.where((dataframeE[u'已誕生多少年'] >= 23) & (dataframeE[u'編程語(yǔ)言'] == 'Java'), '高/是',
                                             '低/否')

# Printing the dataframeE.
print(dataframeE)

#      編程語(yǔ)言  已誕生多少年 分組標(biāo)記(高齡/低齡) 分組標(biāo)記(高齡/低齡,是否是Java)
# 0    Java      23           高                 高/是
# 1  Python      20           低                 低/否
# 2     C++      28           高                 低/否

5、提取數(shù)據(jù)

數(shù)據(jù)提取即是對(duì)符合要求的數(shù)據(jù)完成提取操作,DataFrame對(duì)象提取數(shù)據(jù)主要是按照標(biāo)簽值、標(biāo)簽值和位置以及數(shù)據(jù)位置進(jìn)行提取。

DataFrame對(duì)象按照位置或位置區(qū)域提取數(shù)據(jù),這里所說(shuō)的位置其實(shí)就是DataFrame對(duì)象的索引。

基本上所有的操作都能夠使用DataFrame對(duì)象的loc函數(shù)、iloc函數(shù)這兩個(gè)函數(shù)來(lái)實(shí)現(xiàn)操作。

提取索引為2的DataFrame對(duì)象對(duì)應(yīng)的行數(shù)據(jù)。

# Selecting the row with the index of 2.
res = dataframeE.loc[2]

# Printing the result of the operation.
print(res)

# 編程語(yǔ)言                   C++
# 已誕生多少年                  28
# 分組標(biāo)記(高齡/低齡)              高
# 分組標(biāo)記(高齡/低齡,是否是Java)    低/否
# Name: 2, dtype: object

提取索引0到1位置的所有的行數(shù)據(jù)。

# Selecting the rows with the index of 0 and 1.
res = dataframeE.loc[0:1]

# Printing the result of the operation.
print(res)

#      編程語(yǔ)言  已誕生多少年 分組標(biāo)記(高齡/低齡) 分組標(biāo)記(高齡/低齡,是否是Java)
# 0    Java      23           高                 高/是
# 1  Python      20           低                 低/否

按照前兩行前兩列的數(shù)據(jù)區(qū)域提取數(shù)據(jù)。

# 注意這里帶有冒號(hào):的iloc函數(shù)用法效果是和前面不一樣的。

# Selecting the first two rows and the first two columns.
res = dataframeE.iloc[:2, :2]

# Printing the result of the operation.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 0    Java      23
# 1  Python      20

提取符合條件的數(shù)據(jù)項(xiàng),對(duì)某一列數(shù)據(jù)中指定的值完成提取。

# 提取出編程語(yǔ)言這個(gè)列中數(shù)據(jù)內(nèi)容是Java、C++的數(shù)據(jù)行。

# Selecting the rows where the value in the column `編程語(yǔ)言` is either `Java` or `C++`.
res = dataframeE.loc[dataframeE[u'編程語(yǔ)言'].isin(['Java', 'C++'])]

# Printing the result of the operation.
print(res)

#    編程語(yǔ)言  已誕生多少年 分組標(biāo)記(高齡/低齡) 分組標(biāo)記(高齡/低齡,是否是Java)
# 0  Java      23           高                 高/是
# 2   C++      28           高                 低/否

6、篩選數(shù)據(jù)

篩選數(shù)據(jù)是數(shù)據(jù)處理整個(gè)生命周期中的最后一個(gè)對(duì)原有數(shù)據(jù)的提取操作,通過(guò)各種邏輯判斷條件的操作來(lái)完成數(shù)據(jù)篩選。

這里分別通過(guò)使用DataFrame對(duì)象的'與'、'或'、'非'三種常用的邏輯判斷來(lái)實(shí)現(xiàn)下面的數(shù)據(jù)篩選操作。

# Creating a dataframe with two columns, one called `編程語(yǔ)言` and the other called `已誕生多少年`.
dataframeF = pd.DataFrame({"編程語(yǔ)言": ['Java', 'Python', 'C++'],
                           "已誕生多少年": [23, 20, 28]}, columns=['編程語(yǔ)言', '已誕生多少年'])

res = dataframeF.loc[(dataframeF[u'已誕生多少年'] > 25) & (dataframeF[u'編程語(yǔ)言'] == 'C++'), [u'編程語(yǔ)言', u'已誕生多少年']]

# Printing the result of the operation.
print(res)

#   編程語(yǔ)言  已誕生多少年
# 2  C++      28

res = dataframeF.loc[(dataframeF[u'已誕生多少年'] > 23) | (dataframeF[u'編程語(yǔ)言'] == 'Java'), [u'編程語(yǔ)言', u'已誕生多少年']]

# Printing the result of the operation.
print(res)

#    編程語(yǔ)言  已誕生多少年
# 0  Java      23
# 2   C++      28

res = dataframeF.loc[(dataframeF[u'編程語(yǔ)言'] != 'Java'), [u'編程語(yǔ)言', u'已誕生多少年']]

# Printing the result of the operation.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 1  Python      20
# 2     C++      28

7、數(shù)據(jù)匯總

數(shù)據(jù)匯總通常是使用groupby函數(shù)對(duì)一個(gè)或多個(gè)列名稱進(jìn)行分組,再使用count函數(shù)統(tǒng)計(jì)分組后的數(shù)目。

res = dataframeF.groupby(u'編程語(yǔ)言').count()

# Printing the result of the operation.
print(res)

#         已誕生多少年
# 編程語(yǔ)言
# C++          1
# Java         1
# Python       1

res = dataframeF.groupby(u'編程語(yǔ)言')[u'已誕生多少年'].count()

# Printing the result of the operation.
print(res)

# 編程語(yǔ)言
# C++       1
# Java      1
# Python    1
# Name: 已誕生多少年, dtype: int64

res = dataframeF.groupby([u'編程語(yǔ)言',u'已誕生多少年'])[u'已誕生多少年'].count()

# Printing the result of the operation.
print(res)

# 編程語(yǔ)言    已誕生多少年
# C++     28        1
# Java    23        1
# Python  20        1
# Name: 已誕生多少年, dtype: int64

8、數(shù)據(jù)統(tǒng)計(jì)

數(shù)據(jù)統(tǒng)計(jì)的概念基本上和數(shù)學(xué)上的思路是一樣的,首先是對(duì)數(shù)據(jù)進(jìn)行采樣,采樣完成計(jì)算相關(guān)的標(biāo)準(zhǔn)差、協(xié)方差等相關(guān)的數(shù)據(jù)指標(biāo)。

'''按照采樣不放回的方式,隨機(jī)獲取DataFrame對(duì)象中的兩條數(shù)據(jù)'''
res = dataframeF.sample(n=2, replace=False)

# Printing the result of the operation.
print(res)

#      編程語(yǔ)言  已誕生多少年
# 0    Java      23
# 1  Python      20

可以發(fā)現(xiàn)每次執(zhí)行之后都會(huì)隨機(jī)的從DataFrame的數(shù)據(jù)表中取出兩條數(shù)據(jù)。

若是采樣放回的方式時(shí)則可以將replace的屬性設(shè)置為T(mén)rue即可。

# 計(jì)算出DataFrame對(duì)象的所有列的協(xié)方差
res = dataframeF.cov()

# Printing the result of the operation.
print(res)

#            已誕生多少年
# 已誕生多少年  16.333333

# 計(jì)算出DataFrame對(duì)象相關(guān)性
res = dataframeF.corr()

# Printing the result of the operation.
print(res)

#         已誕生多少年
# 已誕生多少年     1.0

以上就是Python pandas的八個(gè)生命周期總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Python pandas生命周期的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論