1、Pandas DataFrame介紹
1、Pandas是用于數(shù)據(jù)分析的開源Python庫,可以實(shí)現(xiàn)數(shù)據(jù)加載,清洗,轉(zhuǎn)換,統(tǒng)計(jì)處理,可視化等功能
2、DataFrame和Series是Pandas最基本的兩種數(shù)據(jù)結(jié)構(gòu)
3、DataFrame用來處理結(jié)構(gòu)化數(shù)據(jù)(SQL數(shù)據(jù)表,Excel表格)
4、Series用來處理單列數(shù)據(jù),也可以把DataFrame看作由Series對象組成的字典或集合
2、數(shù)據(jù)加載
2.1、查看數(shù)據(jù)類型和屬性
1、初始步驟
import pandas as pd
import pandas as pd
df = pd.read_csv('movie.csv') # csv文件:Comma-Separated Values
df.head()
# tsv文件:Tab-Separated Values
df = pd.read_csv('data/gapminder.tsv', sep='\t')
print(df)
2、查看類型和屬性
# 查看df類型
type(df) # pandas.core.frame.DataFrame
# 查看df的shape屬性,可以獲取DataFrame的行數(shù),列數(shù)
df.shape # (1704, 6)
# 查看df的columns屬性,獲取DataFrame中的列名
df.columns # Index(['country', 'continent', 'year', 'lifeExp', 'pop', 'gdpPercap'], dtype='object')
# 查看df的dtypes屬性,獲取每一列的數(shù)據(jù)類型
df.dtypes
# country object
# continent object
# year int64
# lifeExp float64
# pop int64
# gdpPercap float64
# dtype: object
df.info()
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 1704 entries, 0 to 1703
# Data columns (total 6 columns):
# Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 country 1704 non-null object
# 1 continent 1704 non-null object
# 2 year 1704 non-null int64
# 3 lifeExp 1704 non-null float64
# 4 pop 1704 non-null int64
# 5 gdpPercap 1704 non-null float64
# dtypes: float64(2), int64(2), object(2)
2.2、Pandas與python常用數(shù)據(jù)類型對照

3、數(shù)據(jù)查看
3.1、加載列數(shù)據(jù)
1、加載1列和多列數(shù)據(jù)
#加載1列
country_df = df['country']
country_df.head()
# 加載多列
subset = df[['country', 'year']]
subset.tail()
3.2、按行加載部分?jǐn)?shù)據(jù)
1、行索引
df.head() #加載前5行數(shù)據(jù),最左邊一列是行號,也就是DataFrame的行索引
2、loc:通過行索引獲取指定行數(shù),或者多行數(shù)據(jù)
loc方法傳入行索引,來獲取DataFrame的部分?jǐn)?shù)據(jù)(一行,或多行)
df.loc[0] #單行數(shù)據(jù)
df.loc[[0,99,999]] # 多行數(shù)據(jù)
3、使用tail方法獲取最后一行數(shù)據(jù)
print(df.tail(n=1)) # n = 1控制只顯示1行
4、iloc:通過行號獲取數(shù)據(jù)
在當(dāng)前案例中,使用iloc 和 loc效果是一樣的
需要注意的是,iloc傳入的是索引的序號,loc是索引的標(biāo)簽
使用iloc時可以傳入-1來獲取最后一行數(shù)據(jù),使用loc的時候不行
df.loc[[行號],[列名稱]]
df.iloc[[行號],[列號]]
# 取出所有行,可以使用切片語法 df.loc[ : , [列名]]
subset = df.loc[:, ['year', 'pop']]
# df.iloc[:,[列序號]] # 列序號可以使用-1代表最后一列
# 取出第2列和第4列和最后1列
subset = df.iloc[:, [2,4,-1]]
5、通過range生成序列號
tmp_range = list(range(5))
subset = df.iloc[:, tmp_range]
6、iloc中獲取列數(shù)據(jù)
# 獲取從第3列開始往后獲取到第6列
subset = df.iloc[:,3:6]
# 獲取第0列開始間隔為2往后獲取到第6列
subset = df.iloc[:,0:6:2]
7、指定行列數(shù)據(jù)
df.loc[42, 'country']
df.iloc[42,0]
8、多行多列
df.iloc[[0,99,999], [0,3,5]]
4、分組和聚合計(jì)算
4.1、分組
1、使用DataFrame的groupby方法完成分組/聚合計(jì)算
print(df.groupby('year')['lifeExp'].mean())
# ①通過df.groupby('year')先創(chuàng)一個分組對象
# ②從分組之后的數(shù)據(jù)DataFrameGroupBy中,傳入列名進(jìn)行進(jìn)一步計(jì)算
# ③返回結(jié)果為一個 SeriesGroupBy ,其內(nèi)容是分組后的數(shù)據(jù)
# ④對分組后的數(shù)據(jù)計(jì)算平均值
2、多列值進(jìn)行分組聚合
print(df.groupby(['year', 'continent'])[['lifeExp', 'gdpPercap']].mean())
4.2、分組頻數(shù)計(jì)算
使用 nunique 方法 計(jì)算Pandas Series的唯一值計(jì)數(shù)
使用 value_counts 方法來獲取Pandas Series 的頻數(shù)統(tǒng)計(jì)
df.groupby('continent')['country'].nunique()
# continent
# Africa 52
# Americas 25
# Asia 33
# Europe 30
# Oceania 2
5、繪圖
series的數(shù)據(jù).plot()
默認(rèn)繪制折線圖,index為x軸取值,value 是y的取值
df.groupby('year')['lifeExp'].mean().plot()