您好,登錄后才能下訂單哦!
數據連接(pd.merge)
示例代碼:
import pandas as pd
import numpy as np
df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data1' : np.random.randint(0,10,7)})
df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],
'data2' : np.random.randint(0,10,3)})
print(df_obj1)
print(df_obj2)
運行結果:
data1 key
data1 key
0 8 b
1 8 b
2 3 a
3 5 c
4 4 a
5 9 a
6 6 b
data2 key
0 9 a
1 0 b
2 3 d
示例代碼:
# 默認將重疊列的列名作為“外鍵”進行連接
print(pd.merge(df_obj1, df_obj2))
運行結果:
data1 key data2
0 8 b 0
1 8 b 0
2 6 b 0
3 3 a 9
4 4 a 9
5 9 a 9
示例代碼:
# on顯示指定“外鍵”
print(pd.merge(df_obj1, df_obj2, on='key'))
運行結果:
data1 key data2
0 8 b 0
1 8 b 0
2 6 b 0
3 3 a 9
4 4 a 9
5 9 a 9
示例代碼:
# left_on,right_on分別指定左側數據和右側數據的“外鍵”
# 更改列名
df_obj1 = df_obj1.rename(columns={'key':'key1'})
df_obj2 = df_obj2.rename(columns={'key':'key2'})
print(pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2'))
運行結果:
data1 key1 data2 key2
0 8 b 0 b
1 8 b 0 b
2 6 b 0 b
3 3 a 9 a
4 4 a 9 a
5 9 a 9 a
默認是“內連接”(inner),即結果中的鍵是交集
how指定連接方式
示例代碼:
# “外連接”
print(pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='outer'))
運行結果:
data1 key1 data2 key2
0 8.0 b 0.0 b
1 8.0 b 0.0 b
2 6.0 b 0.0 b
3 3.0 a 9.0 a
4 4.0 a 9.0 a
5 9.0 a 9.0 a
6 5.0 c NaN NaN
7 NaN NaN 3.0 d
示例代碼:
# 左連接
print(pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='left'))
運行結果:
data1 key1 data2 key2
0 8 b 0.0 b
1 8 b 0.0 b
2 3 a 9.0 a
3 5 c NaN NaN
4 4 a 9.0 a
5 9 a 9.0 a
6 6 b 0.0 b
示例代碼:
# 右連接
print(pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='right'))
運行結果:
data1 key1 data2 key2
0 8.0 b 0 b
1 8.0 b 0 b
2 6.0 b 0 b
3 3.0 a 9 a
4 4.0 a 9 a
5 9.0 a 9 a
6 NaN NaN 3 d
suffixes,默認為_x, _y
示例代碼:
# 處理重復列名
df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data' : np.random.randint(0,10,7)})
df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],
'data' : np.random.randint(0,10,3)})
print(pd.merge(df_obj1, df_obj2, on='key', suffixes=('_left', '_right')))
運行結果:
data_left key data_right
0 9 b 1
1 5 b 1
2 1 b 1
3 2 a 8
4 2 a 8
5 5 a 8
left_index=True或right_index=True
示例代碼:
# 按索引連接
df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data1' : np.random.randint(0,10,7)})
df_obj2 = pd.DataFrame({'data2' : np.random.randint(0,10,3)}, index=['a', 'b', 'd'])
print(pd.merge(df_obj1, df_obj2, left_on='key', right_index=True))
運行結果:
data1 key data2
0 3 b 6
1 4 b 6
6 8 b 6
2 6 a 0
4 3 a 0
5 0 a 0
數據合并(pd.concat)
np.concatenate
示例代碼:
import numpy as np
import pandas as pd
arr1 = np.random.randint(0, 10, (3, 4))
arr2 = np.random.randint(0, 10, (3, 4))
print(arr1)
print(arr2)
print(np.concatenate([arr1, arr2]))
print(np.concatenate([arr1, arr2], axis=1))
運行結果:
# print(arr1)
[[3 3 0 8]
[2 0 3 1]
[4 8 8 2]]
# print(arr2)
[[6 8 7 3]
[1 6 8 7]
[1 4 7 1]]
# print(np.concatenate([arr1, arr2]))
[[3 3 0 8]
[2 0 3 1]
[4 8 8 2]
[6 8 7 3]
[1 6 8 7]
[1 4 7 1]]
# print(np.concatenate([arr1, arr2], axis=1))
[[3 3 0 8 6 8 7 3]
[2 0 3 1 1 6 8 7]
[4 8 8 2 1 4 7 1]]
示例代碼:
# index 沒有重復的情況
ser_obj1 = pd.Series(np.random.randint(0, 10, 5), index=range(0,5))
ser_obj2 = pd.Series(np.random.randint(0, 10, 4), index=range(5,9))
ser_obj3 = pd.Series(np.random.randint(0, 10, 3), index=range(9,12))
print(ser_obj1)
print(ser_obj2)
print(ser_obj3)
print(pd.concat([ser_obj1, ser_obj2, ser_obj3]))
print(pd.concat([ser_obj1, ser_obj2, ser_obj3], axis=1))
運行結果:
# print(ser_obj1)
0 1
1 8
2 4
3 9
4 4
dtype: int64
# print(ser_obj2)
5 2
6 6
7 4
8 2
dtype: int64
# print(ser_obj3)
9 6
10 2
11 7
dtype: int64
# print(pd.concat([ser_obj1, ser_obj2, ser_obj3]))
0 1
1 8
2 4
3 9
4 4
5 2
6 6
7 4
8 2
9 6
10 2
11 7
dtype: int64
# print(pd.concat([ser_obj1, ser_obj2, ser_obj3], axis=1))
0 1 2
0 1.0 NaN NaN
1 5.0 NaN NaN
2 3.0 NaN NaN
3 2.0 NaN NaN
4 4.0 NaN NaN
5 NaN 9.0 NaN
6 NaN 8.0 NaN
7 NaN 3.0 NaN
8 NaN 6.0 NaN
9 NaN NaN 2.0
10 NaN NaN 3.0
11 NaN NaN 3.0
示例代碼:
# index 有重復的情況
ser_obj1 = pd.Series(np.random.randint(0, 10, 5), index=range(5))
ser_obj2 = pd.Series(np.random.randint(0, 10, 4), index=range(4))
ser_obj3 = pd.Series(np.random.randint(0, 10, 3), index=range(3))
print(ser_obj1)
print(ser_obj2)
print(ser_obj3)
print(pd.concat([ser_obj1, ser_obj2, ser_obj3]))
運行結果:
# print(ser_obj1)
0 0
1 3
2 7
3 2
4 5
dtype: int64
# print(ser_obj2)
0 5
1 1
2 9
3 9
dtype: int64
# print(ser_obj3)
0 8
1 7
2 9
dtype: int64
# print(pd.concat([ser_obj1, ser_obj2, ser_obj3]))
0 0
1 3
2 7
3 2
4 5
0 5
1 1
2 9
3 9
0 8
1 7
2 9
dtype: int64
# print(pd.concat([ser_obj1, ser_obj2, ser_obj3], axis=1, join='inner'))
# join='inner' 將去除NaN所在的行或列
0 1 2
0 0 5 8
1 3 1 7
2 7 9 9
示例代碼:
df_obj1 = pd.DataFrame(np.random.randint(0, 10, (3, 2)), index=['a', 'b', 'c'],
columns=['A', 'B'])
df_obj2 = pd.DataFrame(np.random.randint(0, 10, (2, 2)), index=['a', 'b'],
columns=['C', 'D'])
print(df_obj1)
print(df_obj2)
print(pd.concat([df_obj1, df_obj2]))
print(pd.concat([df_obj1, df_obj2], axis=1, join='inner'))
運行結果:
# print(df_obj1)
A B
a 3 3
b 5 4
c 8 6
# print(df_obj2)
C D
a 1 9
b 6 8
# print(pd.concat([df_obj1, df_obj2]))
A B C D
a 3.0 3.0 NaN NaN
b 5.0 4.0 NaN NaN
c 8.0 6.0 NaN NaN
a NaN NaN 1.0 9.0
b NaN NaN 6.0 8.0
# print(pd.concat([df_obj1, df_obj2], axis=1, join='inner'))
A B C D
a 3 3 1 9
b 5 4 6 8
數據重構
示例代碼:
import numpy as np
import pandas as pd
df_obj = pd.DataFrame(np.random.randint(0,10, (5,2)), columns=['data1', 'data2'])
print(df_obj)
stacked = df_obj.stack()
print(stacked)
運行結果:
# print(df_obj)
data1 data2
0 7 9
1 7 8
2 8 9
3 4 1
4 1 2
# print(stacked)
0 data1 7
data2 9
1 data1 7
data2 8
2 data1 8
data2 9
3 data1 4
data2 1
4 data1 1
data2 2
dtype: int64
示例代碼:
# 默認操作內層索引
print(stacked.unstack())
# 通過level指定操作索引的級別
print(stacked.unstack(level=0))
運行結果:
# print(stacked.unstack())
data1 data2
0 7 9
1 7 8
2 8 9
3 4 1
4 1 2
# print(stacked.unstack(level=0))
0 1 2 3 4
data1 7 7 8 4 1
data2 9 8 9 1 2
數據轉換
示例代碼:
import numpy as np
import pandas as pd
df_obj = pd.DataFrame({'data1' : ['a'] * 4 + ['b'] * 4,
'data2' : np.random.randint(0, 4, 8)})
print(df_obj)
print(df_obj.duplicated())
運行結果:
# print(df_obj)
data1 data2
0 a 3
1 a 2
2 a 3
3 a 3
4 b 1
5 b 0
6 b 3
7 b 0
# print(df_obj.duplicated())
0 False
1 False
2 True
3 True
4 False
5 False
6 False
7 True
dtype: bool
默認判斷全部列
可指定按某些列判斷
示例代碼:
print(df_obj.drop_duplicates())
print(df_obj.drop_duplicates('data2'))
運行結果:
# print(df_obj.drop_duplicates())
data1 data2
0 a 3
1 a 2
4 b 1
5 b 0
6 b 3
# print(df_obj.drop_duplicates('data2'))
data1 data2
0 a 3
1 a 2
4 b 1
5 b 0
示例代碼:
ser_obj = pd.Series(np.random.randint(0,10,10))
print(ser_obj)
print(ser_obj.map(lambda x : x ** 2))
運行結果:
# print(ser_obj)
0 1
1 4
2 8
3 6
4 8
5 6
6 6
7 4
8 7
9 3
dtype: int64
# print(ser_obj.map(lambda x : x ** 2))
0 1
1 16
2 64
3 36
4 64
5 36
6 36
7 16
8 49
9 9
dtype: int64
示例代碼:
# 單個值替換單個值
print(ser_obj.replace(1, -100))
# 多個值替換一個值
print(ser_obj.replace([6, 8], -100))
# 多個值替換多個值
print(ser_obj.replace([4, 7], [-100, -200]))
運行結果:
# print(ser_obj.replace(1, -100))
0 -100
1 4
2 8
3 6
4 8
5 6
6 6
7 4
8 7
9 3
dtype: int64
# print(ser_obj.replace([6, 8], -100))
0 1
1 4
2 -100
3 -100
4 -100
5 -100
6 -100
7 4
8 7
9 3
dtype: int64
# print(ser_obj.replace([4, 7], [-100, -200]))
0 1
1 -100
2 8
3 6
4 8
5 6
6 6
7 -100
8 -200
9 3
dtype: int64
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。