(编辑:jimmy 日期: 2024/12/27 浏览:2)
import pandas as pd
d = {"sex": ["male", "female", "male", "female"], "color": ["red", "green", "blue", "yellow"], "age": [12, 56, 21, 31]} df = pd.DataFrame(d) df
sex
color
age
map() 会根据提供的函数对指定序列做映射。
map(function, iterable, …)
d = {"male": 1, "female": 0} df["gender"] = df["sex"].map(d) df
sex
color
age
gender
分享pandas数据清洗技巧,在某列山使用replace和正则快速完成值的清洗
d = {"customer": ["A", "B", "C", "D"], "sales": [1000, "950.5RMB", "$400", "$1250.75"]} df = pd.DataFrame(d) df
customer
sales
sales列的数据类型不同意,为后续分析,所以需要将他的格式同统一
df["sales"] = df["sales"].replace("[$,RMB]", "", regex=True).astype("float")
df
customer
sales
查看数据类型
df["sales"].apply(type)
0 <class 'float'>
1 <class 'float'>
2 <class 'float'>
3 <class 'float'>
Name: sales, dtype: object
melt是逆转操作函数,可以将列名转换为列数据(columns name → column values),重构DataFrame,用法如下:
参数说明:
pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name=‘value', col_level=None)
二维表格转成一维表格
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
df = df.melt(id_vars="district_code", var_name="fruit_name", value_name="price") df
district_code
fruit_name
price
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
D、E、F 仅在分类中出现一次,A 出现次数较多。
统计出现次数,并标准化
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
设定阈值
threshold = 0.1 small_categories = frequencies[frequencies < threshold].index small_categories
Index(['E', 'D', 'F'], dtype='object')
替换
df["categories"] = df["categories"].replace(small_categories, "Others")
df
name
categories
例如,假设我们想创建一个正方形列表,例如
squares = [] for x in range(10): squares.append(x**2) squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = list(map(lambda x: x**2, range(10))) squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)] squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
同时还可以利用if来过滤列表
[(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)]
列表推导式可以包含复杂表达式和嵌套函数
from math import pi [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
列表推导式中的初始表达式可以是任意表达式,包括另一个列表推导式。
下面的列表推导式将对行和列进行转置
matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]
[[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
a = 1 b = 2 a, b = b, a print("a = ",a) print("b = ",b)
a = 2
b = 1
sys.getsizeof()
range()函数返回的是一个类,在使用内存方面,range远比实际的数字列表更加高效
import sys mylist = range(1,10000) print(sys.getsizeof(mylist))
48
从Python3.5开始,合并字典的操作更加简单
如果key重复,那么第一个字典的key会被覆盖
d1 ={"a":1,"b":2} d2 = {"b":2,"c":4} m = {**d1,**d2} print(m)
{'a': 1, 'b': 2, 'c': 4}
string = "the author is beishanla" s = string.split(" ") s
['the', 'author', 'is', 'beishanla']
l = ["the","author","is","beishanla"] l = " ".join(l) l
'the author is beishanla'
pip install Pillow
from PIL import Image im = Image.open("E:/Python/00网络爬虫/Project/词云图跳舞视频/aip-python-sdk-4.15.1/pictures/img_88.jpg") im.show()
print(im.format,im.size,im.mode)
JPEG (1920, 1080) RGB
欢迎搜藏,持续更新