sex | color | age | |
---|---|---|---|
0 | male | red | 12 |
1 | female | green | 56 |
2 | male | blue | 21 |
3 | female | yellow | 31 |
map() 會根據提供的函數對指定序列做映射。
map(function, iterable, …)
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 |
分享pandas數據清洗技巧,在某列山使用replace和正則快速完成值的清洗
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列的數據類型不同意,為后續(xù)分析,所以需要將他的格式同統一
df["sales"] = df["sales"].replace("[$,RMB]", "", regex=True).astype("float")
df
customer | sales | |
---|---|---|
0 | A | 1000.00 |
1 | B | 950.50 |
2 | C | 400.00 |
3 | D | 1250.75 |
查看數據類型
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 | |
---|---|---|---|---|
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 |
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 |
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 出現次數較多。
統計出現次數,并標準化
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 | |
---|---|---|
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 |
例如,假設我們想創(chuàng)建一個正方形列表,例如
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
歡迎搜藏,持續(xù)更新
到此這篇關于Pandas數據分析的一些常用小技巧的文章就介紹到這了,更多相關Pandas數據分析技巧內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!