python數(shù)據(jù)分析2:DataFrame對(duì)象

DataFrame對(duì)象:二維表數(shù)據(jù)結(jié)構(gòu),由行列數(shù)據(jù)組成的表格

常用index表示行,columns表示列

import pandas as pd

from pandas.core import series

pd.set_option('display.unicode.east_asian_width',True)

data = [[110, 105, 99], [105, 88, 115], [109, 120, 130]]

index = [0, 1, 2]

columns = ['語(yǔ)文', '數(shù)學(xué)', '英語(yǔ)']

df = pd.DataFrame(data=data, index=index, columns=columns)

# print(df)

'''

? 語(yǔ)文? 數(shù)學(xué)? 英語(yǔ)

0? 110? 105? ? 99

1? 105? ? 88? 115

2? 109? 120? 130

'''

# print(df.columns)? # Index(['語(yǔ)文', '數(shù)學(xué)', '英語(yǔ)'], dtype='object')

# print(df.index)? # Int64Index([0, 1, 2], dtype='int64')

# 遍歷DataFrame數(shù)據(jù)的每一列

for col in df.columns:

? ? series = df[col]

? ? print(series)

'''

0? ? 110

1? ? 105

2? ? 109

Name: 語(yǔ)文, dtype: int64

0? ? 105

1? ? 88

2? ? 120

Name: 數(shù)學(xué), dtype: int64

0? ? 99

1? ? 115

2? ? 130

Name: 英語(yǔ), dtype: int64

'''

1.創(chuàng)建一個(gè)DataFrame對(duì)象

pandas.DataFrame(data,index,columns,dtype,copy)

# data表示數(shù)據(jù),可以是ndarray數(shù)組,series對(duì)象、列表、字典等

# index表示行標(biāo)簽(索引)

# columns表示列標(biāo)簽(索引)

# dtype每一列數(shù)據(jù)的數(shù)據(jù)類型

# copy用于復(fù)制數(shù)據(jù)

# 返回值DataFrame

通過(guò)二維數(shù)組創(chuàng)建成績(jī)表

import pandas as pd

pd.set_option('display.unicode.east_asian_width',True)

data = [[110, 105, 99], [105, 88, 115], [109, 120, 130]]

columns = ['語(yǔ)文', '數(shù)學(xué)', '英語(yǔ)']

df = pd.DataFrame(data=data, columns=columns)

print(df)

'''

? 語(yǔ)文? 數(shù)學(xué)? 英語(yǔ)

0? 110? 105? ? 99

1? 105? ? 88? 115

2? 109? 120? 130

'''

2.通過(guò)字典創(chuàng)建DataFrame對(duì)象

value值只能是一維數(shù)組或單個(gè)的簡(jiǎn)單數(shù)據(jù)類型

# 數(shù)組,則要求所有的數(shù)組長(zhǎng)度一致

# 單個(gè)數(shù)據(jù),每行都需要添加相同數(shù)據(jù)

import pandas as pd

pd.set_option('display.unicode.east_asian_width', True)

df = pd.DataFrame({

? ? '語(yǔ)文':[110, 105, 99],

? ? '數(shù)學(xué)':[105, 88, 115],

? ? '英語(yǔ)':[109, 120, 130],

? ? '班級(jí)':'高一7班'

},index=[0,1,2])

print(df)

'''

? 語(yǔ)文? 數(shù)學(xué)? 英語(yǔ)? ? 班級(jí)

0? 110? 105? 109? 高一7班

1? 105? ? 88? 120? 高一7班

2? ? 99? 115? 130? 高一7班

'''

'''

【DataFrame屬性】

values 查看所有元素的值? df.values

dtypes 查看所有元素的類型? df.dtypes

index 查看所有行名、重命名行名? df.index? ? df.index=[1,2,3]

columns 查看所有列名、重命名列名? df.columns? df.columns=['語(yǔ)','數(shù)']

T 行列數(shù)據(jù)轉(zhuǎn)換? df.T

head 查看前n條數(shù)據(jù),默認(rèn)5條? ? ? ? ? ? ? ? df.head()? df.head(10)

tail 查看后n條數(shù)據(jù),默認(rèn)5條? ? ? ? ? ? ? ? df.tail()? df.tail(10)

shape 查看行數(shù)和列數(shù),[0]表示行,[1]表示列? ? df.shape[0]? df.shape[1]

info 查看索引,數(shù)據(jù)類型和內(nèi)存信息? ? df.info

【DataFrame函數(shù)】

describe 查看每列的統(tǒng)計(jì)匯總信息,DataFrame類型? df.describe()

count? ? 返回每一列中的非空值的個(gè)數(shù)? ? ? ? ? ? ? df.count()

sum? ? ? 返回每一列和和,無(wú)法計(jì)算返回空值? ? ? df.sum()

max? ? ? 返回每一列的最大值? ? ? ? ? ? ? ? df.max()

min? ? ? 返回每一列的最小值? ? ? ? ? ? ? ? df.min()

argmax? 返回最大值所在的自動(dòng)索引位置? ? ? ? df.argmax()

argmin? 返回最小值所在的自動(dòng)索引位置? ? ? ? df.argmin()

idxmax? 返回最大值所在的自定義索引位置? ? ? df.idxmax()

idxmin? 返回最小值所在的自定義索引位置? ? ? df.idxmin()

mean? ? 返回每一列的平均值? ? ? ? ? ? ? ? df.mean()

median? 返回每一列的中位數(shù)? ? ? ? ? ? ? ? df.median()

var? ? ? 返回每一列的方差? ? ? ? ? ? ? ? ? df.var()

std? ? ? 返回每一列的標(biāo)準(zhǔn)差? df.std()

isnull? 檢查df中的空值,空值為T(mén)rue,否則為False,返回布爾型數(shù)組? df.isnull()

notnull? 檢查df中的空值,非空值為T(mén)rue,否則為False,返回布爾型數(shù)組? df.notnull()

中位數(shù)又稱中值,是指按順序排列的一組數(shù)據(jù)中居于中間位置的數(shù)

方差用于度量單個(gè)隨機(jī)變量的離散程序(不連續(xù)程度)

標(biāo)準(zhǔn)差是方差的算術(shù)平方根,反映數(shù)據(jù)集的離散程度

'''

3.導(dǎo)入.xls或.xlsx文件

# pandas.read_excel(io,sheetname=0,header=0,names=None,index_col=None,usecols=None,squeeze=False,dtype=None,engine=None,converters=None,true_values=None,false_values=None,skiprows=None,nrow=None,na_values=None,keep_defalut_na=True,verbose=False,parse_dates=False,date_parser=None,thousands=None,comment=None,skipfooter=0,conver_float=True,mangle_dupe_cols=True,**kwds)

'''

io 字符串,xls或xlsx文件路徑或類文件對(duì)象

sheet_name:None、字符串、整數(shù)、字符串列表或整數(shù)列表,默認(rèn)值為0

? ? 字符串用于工作表名稱;整數(shù)為索引,表示工作表位置

? ? 字符串列表或整數(shù)列表用于請(qǐng)求多個(gè)工作表,為None時(shí)則獲取所有的工作表

? ? sheet_name = 0 第一個(gè)Sheet頁(yè)中的數(shù)據(jù)作為DataFrame對(duì)象

? ? sheet_name = 1 第二個(gè)Sheet頁(yè)中的數(shù)據(jù)作為DataFrame對(duì)象

? ? sheet_name = 'Sheet1' 名為Sheet1的Sheet頁(yè)中的數(shù)據(jù)作為DataFrame對(duì)象

? ? sheet_name = [0,1,'Sheet3'] 第一個(gè),第二個(gè)和名為Sheet3的Sheet頁(yè)中的數(shù)據(jù)作為DataFrame對(duì)象

header:指定作為列名的行,默認(rèn)值為0,即取第一行的值為列名?;驍?shù)據(jù)不包含列名,則為header=None

names:默認(rèn)值為None,要使用的列名列表

index_col:指定列為索引列,默認(rèn)值為None,索引0是DataFrame對(duì)象的行標(biāo)簽

usecols:int、list或字符串,默認(rèn)值為None

? ? 如為None,則解析所有列

? ? 如為int,則解析最后一列

? ? 如為list列表,則解析列號(hào)和列表的列

? ? 如為字符串,則表示以逗號(hào)分隔的Excel列字母和列范圍列表

squeeze:布爾值,默認(rèn)為False,如果解析的數(shù)據(jù)只包含一列,則返回一個(gè)Series

dtype:列的數(shù)據(jù)類型名稱為字典,默認(rèn)值為None

skiprows:省略指定行數(shù)的數(shù)據(jù),從第一行開(kāi)始

skipfooter:省略指定行數(shù)的數(shù)據(jù),從尾部數(shù)的行開(kāi)始

4.導(dǎo)入指定Sheet頁(yè)的數(shù)據(jù)

import pandas as pd

pd.set_option('display.unicode.east_asian_width',True)

df = pd.read_excel('data.xlsx',sheet_name='Sheet1')

print(df.head())

# sheet_name=0表示第一個(gè)sheet頁(yè)的數(shù)據(jù),以此類推,如果不指定,則導(dǎo)入第一頁(yè)

5.指定行索引導(dǎo)入Excel數(shù)據(jù)

import pandas as pd

pd.set_option('display.unicode.east_asian_width',True)

df1 = pd.read_excel('data.xlsx',header=None)

print(df1.head())

'''

Empty DataFrame

Columns: []

Index: [1, 3, 5]

'''

# 導(dǎo)入第一列數(shù)據(jù)

import pandas as pd

pd.set_option('display.unicode.east_asian_width',True)

df1 = pd.read_excel('data.xlsx',usecols=[0])

print(df1.head())

# 導(dǎo)入第一列和第四列

df1 = pd.read_excel('data.xlsx',usecols=[0,3])

'''

Empty DataFrame

Columns: []

Index: [1, 3, 5]

'''

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容