数据分析中pandas的小技巧,快速进行数据预处理,作者:北山啦
import pandas as pd
# https://blog.csdn.net/qq_45176548
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 |
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列的数据类型不同意,为后续分析,所以需要将他的格式同统一
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
1
2
3
Name: sales, dtype: object
参数说明:
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,则使用此级别。
二维表格转成一维表格
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
C 0.181818
B 0.181818
D 0.090909
E 0.090909
F 0.090909
Name: categories, dtype: float64
threshold = 0.1
small_categories = frequencies[frequencies < threshold].index
small_categories
Index(['D', 'E', '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 |
到这里就结束了,如果对你有帮助,欢迎点赞关注,你的点赞对我很重要。作者:北山啦