phpcms中的网站介绍页,怎么请人做网站,lnmp wordpress主题,湖北设计公司文章目录轴向堆叠数据——concat()函数横向堆叠与外连接纵向堆叠与内连接主键合并数据——merge()函数内连接方式外连接方式左连接方式右连接方式其他根据行索引合并数据——join()方法四种连接方式行索引与列索引重叠合并重叠数据——combine_first()方法轴向堆叠数据——conc…
文章目录轴向堆叠数据——concat()函数横向堆叠与外连接纵向堆叠与内连接主键合并数据——merge()函数内连接方式外连接方式左连接方式右连接方式其他根据行索引合并数据——join()方法四种连接方式行索引与列索引重叠合并重叠数据——combine_first()方法轴向堆叠数据——concat()函数 pandas.concat( objs: Union[ Iterable[FrameOrSeriesUnion], Mapping[Optional[Hashable], FrameOrSeriesUnion] ], axis0, join“outer”, ignore_index: bool False, keysNone, levelsNone, namesNone, verify_integrity: bool False, sort: bool False, copy: bool True, ) 上述函数中常用参数表示的含义如下
join表示连接的方式inner表示内连接outer表示外连接默认使用外连接。
ignore_index接受布尔值默认为False。如果设置为True则表示清除现有索引并重置索引值。
keys接受序列表示添加最外层索引。
levels用于构建MultiIndex的特定级别唯一值
names在设置了keys和levels参数后用于创建分层级别的名称
verify_integerity检查新的连接轴是否包含重复项。接收布尔值当设置为True时如果有重复的轴将会抛出错误默认为False 根据轴方向的不同axis参数可以将堆叠分成横向堆叠和纵向堆叠默认采用的是纵向堆叠方式。在堆叠数据时默认采用的是外连接(join参数设为outer)的方式。
横向堆叠与外连接
使用concat()函数合并时若是将axis参数的值设为1且join参数的值设为outer则合并方式为横向堆叠与外连接。
测试对象
left:A B
a A0 B0
b A1 B1
right:C D
c C0 D0
d C1 D1代码
left pd.DataFrame({A: [A0, A1],B: [B0, B1]},index[a, b])
right pd.DataFrame({C: [C0, C1],D: [D0, D1]},index[c, d])
print(pd.concat([left, right], joinouter, axis1))输出结果 A B C D
a A0 B0 NaN NaN
b A1 B1 NaN NaN
c NaN NaN C0 D0
d NaN NaN C1 D1使用concat()函数合并之后产生的不存在的数据将用NaN进行填充。 纵向堆叠与内连接
使用concat()函数合并时若是将axis参数的值设为0且join参数的值设为inner则合并方式为纵向堆叠与内连接。
测试对象
df1:A B C
0 A0 B0 C0
1 A1 B1 C1
2 A2 B2 C2
df2:B C D
0 B3 C3 D3
1 B4 C4 D4
2 B5 C5 D5代码
df1 pd.DataFrame({A: [A0, A1, A2],B: [B0, B1, B2],C: [C0, C1, C2]})
df2 pd.DataFrame({B: [B3, B4, B5],C: [C3, C4, C5],D: [D3, D4, D5]})
print(pd.concat([df1, df2], joininner, axis0))输出结果 B C
0 B0 C0
1 B1 C1
2 B2 C2
0 B3 C3
1 B4 C4
2 B5 C5主键合并数据——merge()函数
主键合并根据一个或多个键将不同的DaraFrame对象连接起来大多数是将两个DataFrame对象中重叠的列作为合并的键。 merge( left, right, how: str “inner”, onNone, left_onNone, right_onNone, left_index: bool False, right_index: bool False, sort: bool False, suffixes(_x, “_y”), copy: bool True, indicator: bool False, validateNone, ) 上述函数中部分参数表示的含义如下
left参与合并的的左侧DataFrame对象right参与合并的的右侧DataFrame对象how表示连接方式默认为inner该参数支持以下的取值 · left使用左侧的DataFrame的键类似于SQL的左外连接 · right使用右侧的DataFrame的键类似于SQL的右外连接 · outer使用两个DataFrame所有的键类似于SQL的全连接 · inner使用两个DataFrame键的交集类似于SQL的内连接on用于连接的列名。必须存在于左右两个DataFrame对象中left_on以左侧的DataFrame作为连接键right_on以右侧的DataFrame作为连接键left_index左侧的行索引用作连接键right_index右侧的行索引用作连接键sort是否排序接受布尔值默认为Falsesuffixes用于追加都重叠列名的末尾默认为(_x,_y)
内连接方式
默认采用howinner的方式合并
测试对象
df1:A B C
0 A0 B0 C0
1 A1 B1 C1
2 A2 B2 C2
df3:B C D
0 B0 C0 D3
1 B2 C2 D4
2 B4 C4 D5代码
df1 pd.DataFrame({A: [A0, A1, A2],B: [B0, B1, B2],C: [C0, C1, C2]})
df3 pd.DataFrame({B: [B0, B2, B4],C: [C0, C2, C4],D: [D3, D4, D5]})
print(pd.merge:\n, pd.merge(df1, df3, on[B, C]))输出结果
pd.merge:A B C D
0 A0 B0 C0 D3
1 A2 B2 C2 D4外连接方式
外连接方式howouterleft与right列中相同的数据将会重叠没有数据的位置使用NaN进行填充。
测试对象
df1:A B C
0 A0 B0 C0
1 A1 B1 C1
2 A2 B2 C2
df3:B C D
0 B0 C0 D3
1 B2 C2 D4
2 B4 C4 D5代码
print(pd.merge(howouter):\n, pd.merge(df1, df3, on[B, C], howouter))输出结果
pd.merge(howouter):A B C D
0 A0 B0 C0 D3
1 A1 B1 C1 NaN
2 A2 B2 C2 D4
3 NaN B4 C4 D5左连接方式
左连接方式howleft以左表作为基准进行连接left表中的数据会全部显示right表中只会显示与重叠数据行索引值相同的数据合并后表中缺失的数据会使用NaN进行填充。
测试对象
df1:A B C
0 A0 B0 C0
1 A1 B1 C1
2 A2 B2 C2
df3:B C D
0 B0 C0 D3
1 B2 C2 D4
2 B4 C4 D5代码
print(pd.merge(howleft):\n, pd.merge(df1, df3, on[B, C], howleft))输出结果
pd.merge(howleft):A B C D
0 A0 B0 C0 D3
1 A1 B1 C1 NaN
2 A2 B2 C2 D4右连接方式
右连接方式howleft以右表作为基准进行连接right表中的数据会全部显示left表中只会显示与重叠数据行索引值相同的数据合并后表中缺失的数据会使用NaN进行填充。
测试对象
df1:A B C
0 A0 B0 C0
1 A1 B1 C1
2 A2 B2 C2
df3:B C D
0 B0 C0 D3
1 B2 C2 D4
2 B4 C4 D5代码
print(pd.merge(howright):\n, pd.merge(df1, df3, on[B, C], howright))测试结果
pd.merge(howright):A B C D
0 A0 B0 C0 D3
1 A2 B2 C2 D4
2 NaN B4 C4 D5其他
即使两张表中的行索引与列索引均没有重叠的部分也可以使用merge()函数来合并。只需要将参数left_index和right_index的值设置为True即可。
测试对象
left:A B
a A0 B0
b A1 B1
right:C D
c C0 D0
d C1 D1代码
print(pd.merge(left_indexright_indexTrue):\n,pd.merge(left, right, howouter, left_indexTrue, right_indexTrue))输出结果 A B C D
a A0 B0 NaN NaN
b A1 B1 NaN NaN
c NaN NaN C0 D0
d NaN NaN C1 D1根据行索引合并数据——join()方法 join(self, other, onNone, how“left”, lsuffix, rsuffix, sortFalse) 上述方法常用参数表示的含义如下
on用于连接列名how可从{‘left’‘right’‘outer’‘inner’}中任选一个默认使用left的方式lsuffix接受字符串用于在左侧重叠的列名后添加后缀名rsuffix接受字符串用于在右侧重叠的列名后添加后缀名sort接受布尔值根据连接键对合并的数据进行排序默认为False
四种连接方式
测试对象
data1:A B C
a A0 B0 C0
b A1 B1 C1
c A2 B2 C2
data2:B C D
b B1 C1 D1
c B2 C2 D2
d B3 C3 D3代码
data1 pd.DataFrame({A: [A0, A1, A2],B: [B0, B1, B2],C: [C0, C1, C2]},index[a, b, c])
data2 pd.DataFrame({B: [B1, B2, B3],C: [C1, C2, C3],D: [D1, D2, D3]},index[b, c, d])
print(data1.join(data2, howouter, lsuffixone):\n,data1.join(data2, howouter, lsuffixone))
print(data1.join(data2, howinner, rsuffixtwo):\n,
data1.join(data2, howinner, rsuffixtwo))
print(data1.join(data2, howleft, lsuffixone):\n,
data1.join(data2, howleft, lsuffixone))
print(data1.join(data2, howright, rsuffixtwo):\n,
data1.join(data2, howright, rsuffixtwo))输出结果
data1.join(data2, howouter, lsuffixone):A Bone Cone B C D
a A0 B0 C0 NaN NaN NaN
b A1 B1 C1 B1 C1 D1
c A2 B2 C2 B2 C2 D2
d NaN NaN NaN B3 C3 D3
data1.join(data2, howinner, rsuffixtwo):A B C Btwo Ctwo D
b A1 B1 C1 B1 C1 D1
c A2 B2 C2 B2 C2 D2
data1.join(data2, howleft, lsuffixone):A Bone Cone B C D
a A0 B0 C0 NaN NaN NaN
b A1 B1 C1 B1 C1 D1
c A2 B2 C2 B2 C2 D2
data1.join(data2, howright, rsuffixtwo):A B C Btwo Ctwo D
b A1 B1 C1 B1 C1 D1
c A2 B2 C2 B2 C2 D2
d NaN NaN NaN B3 C3 D3行索引与列索引重叠
测试对象
join1:A B key
0 A0 B0 K0
1 A1 B1 K1
2 A2 B2 K2
join2:C D
K0 C0 D0
K1 C1 D1
K2 C2 D2代码
join1 pd.DataFrame({A: [A0, A1, A2],B: [B0, B1, B2],key: [K0, K1, K2]})
join2 pd.DataFrame({C: [C0, C1, C2],D: [D0, D1, D2]},index[K0, K1, K2])
print(join1.join(join2, onkey):\n, join1.join(join2, onkey))输出结果
join1.join(join2, onkey):A B key C D
0 A0 B0 K0 C0 D0
1 A1 B1 K1 C1 D1
2 A2 B2 K2 C2 D2合并重叠数据——combine_first()方法
使用combine_first()方法合并两个DataFrame对象时必须确保它们的行索引和列索引有重叠的部分。 combine_first(self, other: “DataFrame”) 上述方法中只有一个参数other该参数用于接收填充缺失值的DataFrame对象。
测试对象
test1:A B
0 NaN B0
1 A1 NaN
2 A2 B2
3 A3 NaN
test2:A B
1 C0 D0
0 C1 D1
2 C2 D2代码
test1 pd.DataFrame({A: [np.nan, A1, A2, A3],B: [B0, np.nan, B2, np.nan]})
test2 pd.DataFrame({A: [C0, C1, C2],B: [D0, D1, D2]},index[1, 0, 2])
print(test1.combine_first(test2):\n, test1.combine_first(test2))输出结果
test1.combine_first(test2):A B
0 C1 B0
1 A1 D0
2 A2 B2
3 A3 NaN从上可知尽管test2表中的行索引与test1表的行索引顺序不同当用test2表的数据替换test1表的NaN值时替换数据与缺失数据的索引位置仍然是相同的。例如test1表中位于第0行第A列的“NaN”需要使用test2表中相同位置的数据“C1来替换。