pandas: 在内存中或对象,会有一套基于对象属性的方法, 可以视为 pandas 是一个存储一维表,二维表,三维表的工具,
主要以二维表为主
一维的表, (系列(Series))
二维的表,DataFrame, 也叫报表
三维的表,(面板(Panel))
文本格式 :
CSV 以文本方式存储, item 之间用逗号分割,记录与记录之间以回车分开 , 可以用 excel 方式打开
json 格式 , 以 key ,value 方式存储
import numpy as np
import pandas as pd
# data 里的 key 可以看成是表头,
data = {
'animal ': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
'age ': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
'visits' : [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'priority' : ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']
}
# 给每一条记录起个别名
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data, index=labels)
print(df)
age animal priority visits
a 2.5 cat yes 1
b 3.0 cat yes 3
c 0.5 snake no 2
d NaN dog yes 3
e 5.0 dog no 2
f 2.0 cat no 3
g 4.5 snake no 1
h NaN cat yes 1
i 7.0 dog no 2
j 3.0 dog no 1
df.head() , head() 默认输出前 5 条记录
df [1:5] 也可以通过切片方式操作 (行索引)
df [['age', 'animal']] (列索引)
df.iloc[0:3, 0:3] 指定行,列输出
age animal priority
a 2.5 cat yes
b 3.0 cat yes
c 0.5 snake no
缺失数据/异常数据处理
Ø 找到缺失值
df[df['age'].isnull()]
填充缺失值
df['age'].fillna(0, inplace=True)
将字符值替换成布尔值
df['priority'] = df['priority'].map({'yes': True, 'no': False})
2.4 可