1-数据准备
1.1-函数
1.2-向量化运算
2-数据处理
2-1 导入
2-1-1 CSV
eg:
from pandas import read_csv;
df = read_csv('C://Users//user//Desktop//4.1//1.csv')
df
2-1-2 文本文件
eg:
from pandas import read_table;
df = read_table('C://Users//user//Desktop//4.1//2.txt', names=['age', 'name'], sep=',')
df
2-1-3 excel
eg:
from pandas import read_excel;
df = read_excel('C://Users//user//Desktop//4.1//3.xlsx', sheetname='data')
2-2 导出
from pandas import DataFrame;
df = DataFrame({
'age': [21, 22, 23],
'name': ['KEN', 'John', 'JIMI']
});
df.to_csv("C:\\Users\\user\\Desktop\\df.csv");
df.to_csv("C:\\Users\\user\\Desktop\\df.csv", index=False);
2-3 重复值处理
2-4 缺失值处理
2-5 空格值处理
eg:
from pandas import read_csv;
df = read_csv('C://Users//user//Desktop//data.csv')
newName = df['name'].str.strip();
df['name'] = newName;
2-6 日期处理
eg:
from pandas import read_csv;
from pandas import to_datetime;
df = read_csv('D:\\Python\\3.5\\1.csv', encoding='utf8')
df_dt = to_datetime(df.注册时间, format='%Y/%m/%d');
eg:
from pandas import read_csv;
from pandas import to_datetime;
from datetime import datetime;
df = read_csv('D:\\Python\\3.5\\1.csv', encoding='utf8')
df_dt = to_datetime(df.注册时间, format='%Y/%m/%d');
df_dt_str = df_dt.apply(lambda x: datetime.strftime(x, '%d/%m/%Y'));
eg:
from pandas import read_csv;
from pandas import to_datetime;
df = read_csv('D:\\Python\\3.5\\1.csv', encoding='utf8')
df_dt = to_datetime(df.注册时间, format='%Y/%m/%d');
df_dt.dt.year;
df_dt.dt.second;
df_dt.dt.minute;
df_dt.dt.hour;
df_dt.dt.day;
df_dt.dt.month;
2-7 字段处理
2-7-1 字段抽取
eg:
from pandas import read_csv;
df = read_csv("D://PA//4.6//data.csv");
df['tel'] = df['tel'].astype(str);
运营商
bands = df['tel'].str.slice(0, 3);
地区
areas = df['tel'].str.slice(3, 7);
号码段
nums = df['tel'].str.slice(7, 11);
2-7-2 字段拆分
2-7-3 记录抽取(条件筛选)
2-7-4 随机抽样
2-8 记录合并
eg:
import pandas;
from pandas import read_csv;
df1 = read_csv("D://PA//4.10//data1.csv", sep="|");
df2 = read_csv("D://PA//4.10//data2.csv", sep="|");
df3 = read_csv("D://PA//4.10//data3.csv", sep="|");
df = pandas.concat([df1, df2, df3])
2-9 字段合并
eg:
from pandas import read_csv;
df = read_csv(
"D://PA//4.11//data.csv",
sep=" ",
names=['band', 'area', 'num']
);
df = df.astype(str);
tel = df['band'] + df['area'] + df['num']
2-10 字段匹配(vlookup)
eg:
import pandas;
from pandas import read_csv;
items = read_csv(
"D://PA//4.12//data1.csv",
sep='|',
names=['id', 'comments', 'title']
);
prices = read_csv(
"D://PA//4.12//data2.csv",
sep='|',
names=['id', 'oldPrice', 'nowPrice']
);
itemPrices = pandas.merge(
items,
prices,
left_on='id',
right_on='id'
);
2-11 简单计算
eg:
from pandas import read_csv;
df = read_csv("D:\\Python\\3.4\\1.csv", sep="|");
result = df.price*df.num
df['sum'] = result
2-12 数据分组
eg:
import pandas;
from pandas import read_csv;
df = read_csv("D:\\PA\\4.15\\data.csv", sep='|');
bins = [min(df.cost)-1, 20, 40, 60, 80, 100, max(df.cost)+1];
labels = ['20以下', '20到40', '40到60', '60到80', '80到100', '100以上'];
pandas.cut(df.cost, bins)
pandas.cut(df.cost, bins, right=False)
pandas.cut(df.cost, bins, right=False, labels=labels)
2-13 日期抽取
eg:
from pandas import read_csv;
from pandas import to_datetime;
df = read_csv('D:\\PA\\4.18\\data.csv', encoding='utf8')
df_dt = to_datetime(df.注册时间, format='%Y/%m/%d');
df_dt.dt.year
df_dt.dt.second;
df_dt.dt.minute;
df_dt.dt.hour;
3-数据分析
3-1 基础分析
3-2 分组分析
3-3 分布分析
eg:
import numpy;
import pandas;
from pandas import read_csv;
df = read_csv('D:\\Python\\4.3\\用户明细.csv');
bins = [min(df.年龄)-1, 20, 30, 40, max(df.年龄)+1];
labels = ['20岁以及以下', '21岁到30岁', '31岁到40岁', '41岁以上'];
年龄分层 = pandas.cut(df.年龄, bins, labels=labels)
df['年龄分层'] = 年龄分层;
df.groupby(by=['年龄分层'])['年龄'].agg({'人数':numpy.size});
3-4 交叉分析
3-5 结构分析
3-6 相关分析
eg:
import numpy;
import pandas;
from pandas import read_csv;
data = read_csv('D:\\Python\\4.6\\data.csv');
--先来看看如何进行两个列之间的相关度的计算
data['人口'].corr(data['文盲率'])
--多列之间的相关度的计算方法
--选择多列的方法
--data.loc[:, ['列1', '列2', '……', '列n']]
data.loc[:, ['超市购物率', '网上购物率', '文盲率', '人口']].corr()