Pandas小技巧
1
|
import pandas as pd |
pandas生成数据
1
2
3
4
5
|
d = { "sex" : [ "male" , "female" , "male" , "female" ], "color" : [ "red" , "green" , "blue" , "yellow" ], "age" : [ 12 , 56 , 21 , 31 ]} df = pd.DataFrame(d) df |
sex | color | age | |
---|---|---|---|
0 | male | red | 12 |
1 | female | green | 56 |
2 | male | blue | 21 |
3 | female | yellow | 31 |
数据替换–map映射
map() 会根据提供的函数对指定序列做映射。
map(function, iterable, …)
- function – 函数
- iterable – 一个或多个序列
1
2
3
|
d = { "male" : 1 , "female" : 0 } df[ "gender" ] = df[ "sex" ]. map (d) df |
sex | color | age | gender | |
---|---|---|---|---|
0 | male | red | 12 | 1 |
1 | female | green | 56 | 0 |
2 | male | blue | 21 | 1 |
3 | female | yellow | 31 | 0 |
数据清洗–replace和正则
分享pandas数据清洗技巧,在某列山使用replace和正则快速完成值的清洗
1
2
3
4
|
d = { "customer" : [ "A" , "B" , "C" , "D" ], "sales" : [ 1000 , "950.5RMB" , "$400" , "$1250.75" ]} df = pd.DataFrame(d) df |
customer | sales | |
---|---|---|
0 | A | 1000 |
1 | B | 950.5RMB |
2 | C | $400 |
3 | D | $1250.75 |
sales列的数据类型不同意,为后续分析,所以需要将他的格式同统一
1
|
df[ "sales" ] = df[ "sales" ].replace( "[$,RMB]" , " ", regex=True).astype(" float ") |
1
|
|
customer | sales | |
---|---|---|
0 | A | 1000.00 |
1 | B | 950.50 |
2 | C | 400.00 |
3 | D | 1250.75 |
查看数据类型
1
|
df[ "sales" ]. apply ( type ) |
0 <class 'float'>
1 <class 'float'>
2 <class 'float'>
3 <class 'float'>
Name: sales, dtype: object
数据透视表分析–melt函数
melt是逆转操作函数,可以将列名转换为列数据(columns name → column values),重构DataFrame,用法如下:
参数说明:
pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name=‘value', col_level=None)
- frame:要处理的数据集。
- id_vars:不需要被转换的列名。
- value_vars:需要转换的列名,如果剩下的列全部都要转换,就不用写了。
- var_name和value_name是自定义设置对应的列名。
- col_level :如果列是MultiIndex,则使用此级别。
二维表格转成一维表格
1
2
3
4
5
6
7
|
d = { "district_code" : [ 12345 , 56789 , 101112 , 131415 ], "apple" : [ 5.2 , 2.4 , 4.2 , 3.6 ], "banana" : [ 3.5 , 1.9 , 4.0 , 2.3 ], "orange" : [ 8.0 , 7.5 , 6.4 , 3.9 ] } df = pd.DataFrame(d) df |
district_code | apple | banana | orange | |
---|---|---|---|---|
0 | 12345 | 5.2 | 3.5 | 8.0 |
1 | 56789 | 2.4 | 1.9 | 7.5 |
2 | 101112 | 4.2 | 4.0 | 6.4 |
3 | 131415 | 3.6 | 2.3 | 3.9 |
1
2
3
4
|
df = df.melt(id_vars = "district_code" , var_name = "fruit_name" , value_name = "price" ) df |
district_code | fruit_name | price | |
---|---|---|---|
0 | 12345 | apple | 5.2 |
1 | 56789 | apple | 2.4 |
2 | 101112 | apple | 4.2 |
3 | 131415 | apple | 3.6 |
4 | 12345 | banana | 3.5 |
5 | 56789 | banana | 1.9 |
6 | 101112 | banana | 4.0 |
7 | 131415 | banana | 2.3 |
8 | 12345 | orange | 8.0 |
9 | 56789 | orange | 7.5 |
10 | 101112 | orange | 6.4 |
11 | 131415 | orange | 3.9 |
将分类中出现次数较少的值归为others
1
2
3
4
5
6
|
d = { "name" : [ 'Jone' , 'Alica' , 'Emily' , 'Robert' , 'Tomas' , 'Zhang' , 'Liu' , 'Wang' , 'Jack' , 'Wsx' , 'Guo' ], "categories" : [ "A" , "C" , "A" , "D" , "A" , "B" , "B" , "C" , "A" , "E" , "F" ]} df = pd.DataFrame(d) df |
name | categories | |
---|---|---|
0 | Jone | A |
1 | Alica | C |
2 | Emily | A |
3 | Robert | D |
4 | Tomas | A |
5 | Zhang | B |
6 | Liu | B |
7 | Wang | C |
8 | Jack | A |
9 | Wsx | E |
10 | Guo | F |
D、E、F 仅在分类中出现一次,A 出现次数较多。
统计出现次数,并标准化
1
2
|
frequencies = df[ "categories" ].value_counts(normalize = True ) frequencies |
A 0.363636
B 0.181818
C 0.181818
E 0.090909
D 0.090909
F 0.090909
Name: categories, dtype: float64
设定阈值
1
2
3
|
threshold = 0.1 small_categories = frequencies[frequencies < threshold].index small_categories |
1
|
Index([ 'E' , 'D' , 'F' ], dtype = 'object' ) |
替换
1
|
df[ "categories" ] = df[ "categories" ].replace(small_categories, "Others" ) |
1
|
|
name | categories | |
---|---|---|
0 | Jone | A |
1 | Alica | C |
2 | Emily | A |
3 | Robert | Others |
4 | Tomas | A |
5 | Zhang | B |
6 | Liu | B |
7 | Wang | C |
8 | Jack | A |
9 | Wsx | Others |
10 | Guo | Others |
Python小技巧
列表推导式
例如,假设我们想创建一个正方形列表,例如
1
2
3
4
|
squares = [] for x in range ( 10 ): squares.append(x * * 2 ) squares |
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
1
2
|
squares = list ( map ( lambda x: x * * 2 , range ( 10 ))) squares |
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
1
2
|
squares = [x * * 2 for x in range ( 10 )] squares |
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
同时还可以利用if来过滤列表
1
|
[(x, y) for x in [ 1 , 2 , 3 ] for y in [ 3 , 1 , 4 ] if x ! = y] |
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
列表推导式可以包含复杂表达式和嵌套函数
1
2
|
from math import pi [ str ( round (pi, i)) for i in range ( 1 , 6 )] |
['3.1', '3.14', '3.142', '3.1416', '3.14159']
列表推导式中的初始表达式可以是任意表达式,包括另一个列表推导式。
下面的列表推导式将对行和列进行转置
1
2
3
4
5
|
matrix = [ [ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ], ] |
1
|
[[row[i] for row in matrix] for i in range ( 4 )] |
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
交换变量
1
2
3
4
5
|
a = 1 b = 2 a, b = b, a print ( "a = " ,a) print ( "b = " ,b) |
a = 2
b = 1
检查对象使用内存情况
sys.getsizeof()
range()函数返回的是一个类,在使用内存方面,range远比实际的数字列表更加高效
1
2
3
|
import sys mylist = range ( 1 , 10000 ) print (sys.getsizeof(mylist)) |
48
合并字典
从Python3.5开始,合并字典的操作更加简单
如果key重复,那么第一个字典的key会被覆盖
1
2
3
4
|
d1 = { "a" : 1 , "b" : 2 } d2 = { "b" : 2 , "c" : 4 } m = { * * d1, * * d2} print (m) |
{'a': 1, 'b': 2, 'c': 4}
字符串分割成列表
1
2
3
|
string = "the author is beishanla" s = string.split( " " ) s |
['the', 'author', 'is', 'beishanla']
字符串列表创建字符串
1
2
3
|
l = [ "the" , "author" , "is" , "beishanla" ] l = " " .join(l) l |
'the author is beishanla'
Python查看图片
1
|
pip install Pillow |
1
2
3
|
from PIL import Image im = Image. open ( "E:/Python/00网络爬虫/Project/词云图跳舞视频/aip-python-sdk-4.15.1/pictures/img_88.jpg" ) im.show() |
1
|
print (im. format ,im.size,im.mode) |
JPEG (1920, 1080) RGB
欢迎搜藏,持续更新
总结
到此这篇关于Pandas数据分析的一些常用小技巧的文章就介绍到这了,更多相关Pandas数据分析技巧内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_45176548/article/details/113649094