Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e7b1e8

Browse files
committed
提交代码
1 parent f903b98 commit 9e7b1e8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

‎qingxiangke/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ Python技术 公众号文章代码库
1313

1414
[爬虫](https://github.com/JustDoPython/python-examples/tree/master/qingxiangke/Crawler) : 用爬虫代替工作的魅力
1515

16-
[pandas](https://github.com/JustDoPython/python-examples/tree/master/qingxiangke/PandasSift) : pandas的多条件筛选
16+
[pandas条件筛选](https://github.com/JustDoPython/python-examples/tree/master/qingxiangke/PandasSift) : pandas的多条件筛选
17+
18+
[pandas的多表连接](https://github.com/JustDoPython/python-examples/tree/master/qingxiangke/pandasMerge) : pandas的多表连接

‎qingxiangke/pandasMerge/main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pandas as pd
2+
3+
df1 = pd.DataFrame({
4+
'姓名': ['张三', '李四', '王五', '刘六', '齐四'],
5+
'号码': ['123', '456', '789', '987', '654']
6+
})
7+
8+
df2 = pd.DataFrame({
9+
'姓名': ['张三', '张三', '张三', '李四', '李四', '李四', '李四', '王五', '王五', '刘玉', '胡军', '刘玉', '刘六', '刘六', '刘六', '刘六', '刘克', '刘玉', '齐七', '齐七', '齐七', '齐七', '冯亮', '刘玉', '王云'],
10+
11+
'号码': ['123', '123', '123', '123', '123', '456', '456', '456', '456', '456', '741', '741', '741', '741', '741', '789', '789', '789', '789', '789', '852', '852', '852', '852', '852'],
12+
13+
'日期': ['2022年03月13日', '2022年03月06日', '2022年01月30日', '2022年01月04日', '2022年02月26日', '2022年03月26日', '2022年03月06日', '2022年01月30日', '2022年01月29日', '2022年03月13日', '2022年03月06日', '2022年02月19日', '2022年02月04日', '2022年03月10日', '2022年04月19日', '2022年03月10日', '2022年01月29日', '2022年02月19日', '2022年03月06日', '2022年03月26日', '2022年01月04日', '2022年02月04日', '2022年04月19日', '2022年02月26日', '2022年03月06日'],
14+
15+
'方案': ['G1012', 'G1022', 'G1002', 'G1007', 'G1017', 'G1023', 'G1018', 'G1003', 'G1008', 'G1013', 'G1020', 'G1015', 'G1010', 'G1005', 'G1025', 'G1004', 'G1009', 'G1014', 'G1019', 'G1024', 'G1006', 'G1011', 'G1026', 'G1016', 'G1021']
16+
})
17+
18+
# how默认为"inner":内连接查询特点是有匹配的才显示,不匹配的不显示
19+
df = pd.merge(left=df1, right=df2, on="姓名", how="inner")
20+
21+
# how="outer"为外连接:查询特点是无论匹不匹配都显示,对应的值没有则显示空
22+
df = pd.merge(left=df1, right=df2, on="姓名", how="outer")
23+
24+
# how="left"为左连接:查询表示左边的值全部显示,如右边无匹配则显示空。但是右边有的值匹配不了左边则不显示
25+
df = pd.merge(left=df1, right=df2, on="姓名", how="left")
26+
27+
# how="right"为右连接:与左连接相反
28+
df = pd.merge(left=df1, right=df2, on="姓名", how="right")
29+
30+
# 如果和表合并的过程中遇到有一列两个表都同名,但是值不同,合并的时候又都想保留下来,就可以用suffixes给每个表的重复列名增加后缀。suffixes=['_1','_r']
31+
df = pd.merge(left=df1, right=df2, on="姓名", how="right")
32+
33+
print(df)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /