pandas
pandas是基于Numpy的一種工具,該工具是為解決資料分析任務而創建的,pandas納入了大量庫和一些標準的資料模型,提供了高效的操作大型資料集所需要的的工具,pandas提供了大量能使我們快速便捷地處理資料的函式和方法,
- 表格資料操作(增刪改查)
- 實作多個表格的處理
- 資料清洗操作:缺失值、重復值、例外值、資料標準化、資料轉換的操作
- 實作所有的excel的特殊操作:生成透視表、交叉表
- 完成統計分析
一、pandas的創建
import pandas as pd
1、表結構資料,構建Dataframe
Columns:列索引 index:行索引 values:元素資料
方式一:
df = pd.DataFrame(
data=[['alex', 20, '男','0831'],['tom', 30, '女', '0830'],],
index=['a','b'], # 可以不寫,默認從0開始,也可以直接指定字符進行排序
columns=['name', 'age', 'sex', 'class'],
) # 構建方法
print(df) # 列印資料
name age sex class
a alex 20 男 0831
b tom 30 女 0830
方式二:
df1 = pd.DataFrame(data={'name':['tom', 'alex'], 'age':[18,20], 'sex':['男','女'], 'class':['0831','0831']})
print(df) # 列印資料,沒有指定index字符排序時,默認從0開始排序
name age sex class
0 alex 20 男 0831
1 tom 30 女 0830
二、Dataframe屬性
- 結構:shape
print('結構',df.shape) #
結構 (2, 4)
- 元素個數:size
print('元素個數',df.size) #
元素個數 8
- 資料型別:dtypes
print('資料型別\n', df.dtypes) # 除了int float外其他型別全為object
型別資料型別
name object
age int64
sex object
class object
- 維度:ndim
print('維度', df.ndim) #
維度 2
- 列索引:columns
print('列索引', df.columns)
列索引 Index(['name', 'age', 'sex', 'class'], dtype='object')
- 行索引
print('行索引', df.index)
行索引 Index(['a', 'b'], dtype='object')
- 資料:values
print('資料型別', df.values)
資料型別
[['alex' 20 '男' '0831']
['tom' 30 '女' '0830']]
三、df的查找
- 索引某一列值
df1[‘name’] 一維的切法,回傳的是series
print(df1['name']) # 切一列值的方法
0 tom
1 alex
- 切多列值的方法
print(df1[['name', 'age']])
name age
0 tom 18
1 alex 20
print(type(df1[['name', 'age']])) # series 是一維的型別,只有一個軸
<class 'pandas.core.series.Series'>
- 索引切的方法
方法一:
print(df[['name', 'age']][:2]) # 不能指定行進行索引
name age
a alex 20
b tom 30
方式二:
索引切的方法: df.loc[行索引名稱、條件, 列的索引名稱]
print(df.loc['a', 'name'])
alex
df.loc['a', ['name']] # <class 'pandas.core.series.Series'> 行或者列,只要有一個為字串,是一維
df.loc[['a'], ['name']] # <class 'pandas.core.frame.DataFrame'> 行或者列,兩個引數都為串列,是二維
- 條件索引: bool 切片
mask = df['age']>18 # 回傳所有大于18歲的同學,回傳True, False
mask2 = df['sex'] == '女' # 回傳所有女的同學
mask3 = mask & mask2 # 將兩個mask進行結合,不能使用and,只能使用 & 邏輯與
print(mask3)
a False
b True
dtype: bool
print(df.loc[mask3, :]) # 利用mask,對資料進行切片
name age sex class
b tom 30 女 0830
- 索引查詢: iloc 【行的索引, 列的索引】 # 前閉后開
print(df.iloc[:1, :])
name age sex class
a alex 20 男 0831
df增加方法
- 鍵值對添加列
# df['address'] = ['北京', '上海'] 兩種方式,一一對應, 直接等于‘北京’,則所有資料都會變成北京
df['address'] = '北京'
name age sex class address
a alex 20 男 0831 北京
b tom 30 女 0830 北京
- append增加行
df_mini = pd.DataFrame(data = {
'name':['jerry', 'make'],
'age':[15, 18],
'sex':['男', '女'],
'class':['0831', '0770'],
'address':['北京', '河南']
}, index = ['a', 'b'])
df4 = df.append(df_mini)
print(df4)
a alex 20 男 0831 北京
b tom 30 女 0830 北京
a jerry 15 男 0831 北京
b make 18 女 0770 河南
五、洗掉方法
axis : 洗掉的行或者列
inplace:是否修改原始表
a = df4.drop(labels=['address', 'class'], axis=1) # 洗掉列 需要使用一個變數接受
df4.drop(labels=['a'], axis=0, inplace=True)
六、修改
切出指定資料,再進行賦值修改
c = df4.loc[df4['name'] == 'tom', 'class'] = '有問題'
print(c)
name age sex class address
a alex 20 男 0831 北京
b tom 30 女 有問題 北京
a jerry 15 男 0831 北京
b make 18 女 0770 河南
七、統計分析
- 延用了Numpy中的10個統計方法
min() argmin()
max() argmax()
std() vat()
sum() mean()
cumsum() cumprod()
- pandas中的方法
df['age'].min()
df['age'].max()
df['age'].argsort()
- 眾數、非空元素、頻數
df['age'].mode()
a grade
b grade
dtype: object
df['age'].count()
tom 1
make 1
alex 1
jerry 1
Name: name, dtype: int64
df['age'].value_counts()
name alex
age 20
sex 女
class 0830
address 北京
dtype: object
- 針對df型別
df['age'].idxmax(axis=1) # 橫向比較
df['age'].idxmax(axis=0) # 縱向比較
name age sex class address
0 alex 15 女 0831 北京
1 jerry 18 男 NaN NaN
2 make 20 NaN NaN NaN
3 tom 30 NaN NaN NaN
- 描述describe
df['age'].describe()
# age
# count 4.00 非空數目
# mean 20.75 平均值
# std 6.50 標準差
# min 15.00 最小
# 25% 17.25 1/4
# 50% 19.00 2/4
# 75% 22.50 3/4
# max 30.00 最大
df['name'].describe()
# count : 非空數目
# unique: 去重之后有幾個值
# top: 眾數
# freq: 眾數出現的頻數
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/242888.html
標籤:python
上一篇:numpy
