alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【452】pandas筛选出表中满足另一个表所有条件的数据

参考:pandas筛选出表中满足另一个表所有条件的数据

参考:pandas:匹配两个dataframe

使用 pd.merge 来实现

on 表示查询的 columns,如果都有 id,那么这是很好的区别项,找到 id 相同的进行merge。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
>>> import numpy as np
            
>>> import pandas as pd
            
>>> data1 = {
    'one': pd.Series([1,2,3]),
    'two': pd.Series([11,22,33])
    }
            
>>> df1 = pd.DataFrame(data = data1)
            
>>> df1
            
   one  two
0    1   11
1    2   22
2    3   33
>>> data2 = {
    'one': pd.Series([1,2,3,4,5,6]),
    'two': pd.Series([11,22,33]),
    'three': pd.Series([111,222,333]),
    'four': pd.Series([1111,2222,3333,4444,5555,6666])
    }
                   
>>> df2 = pd.DataFrame(data = data2)
            
>>> df2
            
   one   two  three  four
0    1  11.0  111.0  1111
1    2  22.0  222.0  2222
2    3  33.0  333.0  3333
3    4   NaN    NaN  4444
4    5   NaN    NaN  5555
5    6   NaN    NaN  6666
>>> df2[df2['one']<3]
            
   one   two  three  four
0    1  11.0  111.0  1111
1    2  22.0  222.0  2222
 
>>> df = pd.merge(df1, df2, how='inner')
              
>>> df
              
   one  two  three  four
0    1   11  111.0  1111
1    2   22  222.0  2222
2    3   33  333.0  3333
>>> df1
              
   one  two
0    1   11
1    2   22
2    3   33
>>> df2
              
   one   two  three  four
0    1  11.0  111.0  1111
1    2  22.0  222.0  2222
2    3  33.0  333.0  3333
3    4   NaN    NaN  4444
4    5   NaN    NaN  5555
5    6   NaN    NaN  6666
>>> pd.merge(df1, df2, how='inner')
              
   one  two  three  four
0    1   11  111.0  1111
1    2   22  222.0  2222
2    3   33  333.0  3333
>>> pd.merge(df2, df1, how='inner')
              
   one   two  three  four
0    1  11.0  111.0  1111
1    2  22.0  222.0  2222
2    3  33.0  333.0  3333
>>> five = pd.Series([1,2,3,4,5,6])
              
>>> df2['five'] = five
              
>>> df2
              
   one   two  three  four  five
0    1  11.0  111.0  1111     1
1    2  22.0  222.0  2222     2
2    3  33.0  333.0  3333     3
3    4   NaN    NaN  4444     4
4    5   NaN    NaN  5555     5
5    6   NaN    NaN  6666     6
>>> df1
              
   one  two
0    1   11
1    2   22
2    3   33
>>> pd.merge(df2, df1, how='inner')
              
   one   two  three  four  five
0    1  11.0  111.0  1111     1
1    2  22.0  222.0  2222     2
2    3  33.0  333.0  3333     3
>>> pd.merge(df1, df2, how='inner')
              
   one  two  three  four  five
0    1   11  111.0  1111     1
1    2   22  222.0  2222     2
2    3   33  333.0  3333     3
>>> df1
              
   one  two
0    1   11
1    2   22
2    3   33
>>> df2
              
   one   two  three  four  five
0    1  11.0  111.0  1111     1
1    2  22.0  222.0  2222     2
2    3  33.0  333.0  3333     3
3    4   NaN    NaN  4444     4
4    5   NaN    NaN  5555     5
5    6   NaN    NaN  6666     6
>>> six = pd.Series([-1, -2, -3])
              
>>> df1['six'] = six
              
>>> df1
              
   one  two  six
0    1   11   -1
1    2   22   -2
2    3   33   -3
>>> df2
              
   one   two  three  four  five
0    1  11.0  111.0  1111     1
1    2  22.0  222.0  2222     2
2    3  33.0  333.0  3333     3
3    4   NaN    NaN  4444     4
4    5   NaN    NaN  5555     5
5    6   NaN    NaN  6666     6
>>> pd.merge(df1, df2, how='inner')
              
   one  two  six  three  four  five
0    1   11   -1  111.0  1111     1
1    2   22   -2  222.0  2222     2
2    3   33   -3  333.0  3333     3
>>> pd.merge(df2, df1, how='inner')
              
   one   two  three  four  five  six
0    1  11.0  111.0  1111     1   -1
1    2  22.0  222.0  2222     2   -2
2    3  33.0  333.0  3333     3   -3

 

posted on   McDelfino  阅读(1203)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-11-16 【269】蓝牙键盘连接
2015-11-16 【177】IDL常见问题解答
2011-11-16 【002】◀▶ C#学习(一) - C#编程基础
2011-11-16 【001】学习计划 - 开始学习C#,用C#
点击右上角即可分享
微信分享提示