于贵洋的博客

BI、数据分析


  • 首页

  • 分类

  • 标签

  • 归档

  • 站点地图

  • 公益404

  • 关于

  • 搜索

小白学习Tableau-购物篮分析

发表于 2017-08-01 | 分类于 Tableau

Tableau实例

关于购物篮的介绍可以参考数据分析案例-购物篮分析

目标

我们想要分析的是顾客在购买商品的时候,哪些商品会同时购买。

下面,我们直接开始开发Tableau

数据源

我们就是用Tableau默认自带的“示例-超市”

拖入两张订单表

因为我们需要分析的是每个顾客,在购买A商品的时候,还会购买哪些商品。只使用一张订单表,不是很容易看出来,所以我们需要拖入2张订单表,以此来更方便的处理数据。
因为我们要分析的是每个顾客,所以我们使用顾客ID去关联

阅读全文 »

Python基础(3)- lambda表达式

发表于 2017-08-01 | 分类于 Python-基础

这里简单整理下,lambda表达式相关内容。

什么是lambda表达式

lambda表达式,是一个匿名函数,用起来方便快捷一些

1
2
3
4
5
6
#lambda 参数:操作(参数)
fun_add = lambda x: x+1
print(fun_add(1))
print(fun_add(10))

这里,一个简单的加1的函数,看起来也很直观

1
2
3
4
fun_add = lambda x,y: x+y
print(fun_add(3,4))
print(fun_add(8,9))

这是x+y的函数,的确简洁很多
看网上,提到lambda表达式的话,都会提到函数式编程,一些常用的函数,像map,reduce,filter,sorted,

map函数

map是Python内置的一个函数,接收2个参数,一个函数,一个或多个可迭代参数

1
2
3
4
map(func, *iterables) --> map object
Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.

1
2
3
4
5
6
7
8
d1 = [1,2,3,4,5]
def add(x):
return x+10
t = map(add , d1)
print('原来的list:',d1)
print('执行add后的list:',list(t))

我们定义了一个函数,对传入的参数加10,一个list

map把这个函数,作用在每一个list的元素上,
这里呢,我们就可以用lambda表达式写,方便又直观

1
2
3
4
d1 = [1,2,3,4,5]
t = map(lambda x: x+10 , d1)
print('原来的list:',d1)
print('执行add后的list:',list(t))

我们也可以传2个list,这里会计算2个list的和

1
2
3
4
s1 = [1,2,3]
s2 = [4,5,6]
t = map(lambda x,y: x+y,s1,s2)
print(list(t)) ##[5, 7, 9]

reduce函数

reduce会将function作用于sequence,function接收2个参数

1
2
3
4
5
6
7
8
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

1
2
3
4
5
t = reduce(lambda x,y: x+y, [1,2,3])
print(t) #6,((1+2)+3)=6
t = reduce(lambda x,y: x*10+y,[1,2,3])
print(t) # ((1*10+2)*10+3)=123

filter函数

看名字,就是一个过滤的功能,对每个item调用function,只返回为True的

1
2
3
4
| filter(function or None, iterable) --> filter object
|
| Return an iterator yielding those items of iterable for which function(item)
| is true. If function is None, return the items that are true.

1
2
t = filter(lambda x: x<0,range(-5,5))
print(list(t)) #[-5, -4, -3, -2, -1]

Pandas手册(5)- 用pandas完成excel中常见任务

发表于 2017-08-01 | 分类于 Python-Pandas

Python
Pandas


发现了一篇很好的教程,介绍一些Excel中的常用操作,怎样在pandas中实现,很不错,这里学习,顺便分享下。
原文地址:用Pandas完成Excel中常见的任务,

这个是翻译的,再原文是:Common Excel Tasks Demonstrated in Pandas

好了,下面,我们开始学习下。

基础数据

这个是从网上找的一个成绩单,拿了一部分数据

首先呢,我们想要,在加一列,显示总分,Excel中很方便

在pandas中呢,其实,我们就是需要“数学”,“语文”,“英语”这3列加在一起,我们怎样获取这3列呢?
前面,我们说过在DataFrame中,怎样去筛选数据

1
2
3
4
5
6
7
8
import pandas as pd
import numpy as np
df = pd.read_excel(r'D:\document\tableau_data\data_stu.xlsx',sheetname=0)
print(df)
print(df['数学'])
print(df['语文'])

阅读全文 »

Python基础(2)- 格式化format

发表于 2017-08-01 | 分类于 Python-基础

刚刚在练习pandas的时候,遇到一个格式化的问题,没有太理解,百度了下,这里整理下。
str.format(),是一个格式化字符串的函数,很强大

str.format(args, *kwargs)

主要是使用 {}和:
这里直接就复制过来了,我们可以通过参数的位置来输出

1
2
3
4
5
6
7
8
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
'hello world'
>>> "{0} {1}".format("hello", "world") # 设置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
'world hello world'

我们也可以通过key,value的形式来格式化

1
2
3
4
5
6
7
'name:{name},age:{age}'.format(name='lufei',age=20)
Out[46]: 'name:lufei,age:20'
p=['namei',20]
'name:{0[0]},age:{0[1]}'.format(p)
Out[51]: 'name:namei,age:20'

填充与对齐

1
2
3
4
5
6
7
8
9
10
11
12
13
^、<、>分别是居中、左对齐、右对齐,后面带宽度
:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充
#右对齐,长度为8,不够的默认用空格补全
'{:>8}'.format('189')
Out[52]: ' 189'
#右对齐,长度为8,不够的用0补全
'{:0>8}'.format('189')
Out[53]: '00000189'
'{:a>8}'.format('189')
Out[54]: 'aaaaa189'

数值精度
这个说的挺全的,直接截图来吧

参考资料:
Python format 格式化函数
官方介绍:Format String Syntax
飘逸的python - 增强的格式化字符串format函数

Pandas手册(4)- 对数据进行筛选和排序

发表于 2017-07-31 | 分类于 Python-Pandas

Python
Pandas


前几天看了篇教程:使用Pandas对数据进行筛选和排序

里面主要介绍了,我们在使用Pandas时,对数据进行筛选和排序的介绍
这里简单总结分享下自己。

排序

可能是版本的问题,原文中的sort函数没有了,变成了2个常用的函数 sort_index和sort_value

1
2
3
4
5
DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, by=None)
Sort object by labels (along an axis)
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
Sort by the values along either axis

sort_index:按照索引排序,及列标签或行标签,axis=0是列标签,axis=1是行标签

1
2
3
4
5
6
7
df3 = pd.DataFrame(np.random.randn(6,4),
index=list(range(0,12,2)),
columns=list(range(0,8,2)))
print(df3)
print(df3.sort_index(axis=0,ascending=False))
print(df3.sort_index(axis=1,ascending=False))

sort_value:按值进行排序,这个估计用的会多些,按数据内容进行排序

1
2
3
print(df3)
print(df3.sort_values(by=[0],axis=0,ascending=True))
print(df3.sort_values(by=[2],axis=0,ascending=False))

阅读全文 »
1…161718…23
于贵洋

于贵洋

111 日志
17 分类
30 标签
RSS
GitHub
友情链接
  • 很久之前的CSDN博客
0%
© 2017 于贵洋
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.3
Hosted by GitHub Pages
本站访客数 人次 本站总访问量 次