数据转换
import numpy as np
import pandas as pd
重复数据
df_obj = pd.DataFrame({'data1' : ['a'] * 4 + ['b'] * 4,
'data2' : np.random.randint(0, 4, 8)})
df_obj
df_obj.duplicated()
0 False
1 False
2 True
3 True
4 False
5 False
6 True
7 False
dtype: bool
df_obj.drop_duplicates()
df_obj.drop_duplicates('data2')
map函数
ser_obj = pd.Series(np.random.randint(0,10,10))
ser_obj
0 5
1 9
2 3
3 9
4 6
5 1
6 5
7 4
8 1
9 3
dtype: int32
ser_obj.map(lambda x : x ** 2)
0 25
1 81
2 9
3 81
4 36
5 1
6 25
7 16
8 1
9 9
dtype: int64
数据替换repalce
# 替换单个值
ser_obj.replace(0, -100)
0 5
1 9
2 3
3 9
4 6
5 1
6 5
7 4
8 1
9 3
dtype: int32
# 替换多个值
ser_obj.replace([0, 2], -100)
0 5
1 9
2 3
3 9
4 6
5 1
6 5
7 4
8 1
9 3
dtype: int32
# 替换多个值
ser_obj.replace([0, 2], [-100, -200])
0 5
1 9
2 3
3 9
4 6
5 1
6 5
7 4
8 1
9 3
dtype: int32
网友评论