如何基于groupby.groups.keys()过滤熊猫groupby对象

时间:2019-04-09 16:52:12

标签: python-3.x filter pandas-groupby concat

我有熊猫数据框df1和df2

df1:
     City  Pop Homes Other
0  City_1  100     1     0
1  City_1  100     2     6
2  City_1  100     2     2
3  City_1  100     3     9
4  City_1  200     1     6
5  City_1  200     2     6
6  City_1  200     3     7
7  City_1  300     1     0

df2:
     City  Pop Homes Other
0  City_1  100     1     0
1  City_1  100     2     6
2  City_1  100     2     2
3  City_1  100     8     9
4  City_1  200     1     6
5  City_1  200     2     6
6  City_1  800     3     7
7  City_1  800     8     0

我想创建具有与df1和df2相同列的df3,但仅包含配对的Pop和Homes值相同的行。

df3:
     City  Pop Homes Other
0  City_1  100     1     0
1  City_1  100     2     6
2  City_1  100     2     2
4  City_1  200     1     6
5  City_1  200     2     6

要获得df1和df2中的配对,我已经完成:

df1_string = """
City_1      100      1     0
City_1      100      2     6
City_1      100      2     2
City_1      100      3     9
City_1      200      1     6
City_1      200      2     6
City_1      200      3     7
City_1      300      1     0"""

df2_string = """
City_1      100      1     0
City_1      100      2     6
City_1      100      2     2
City_1      100      8     9
City_1      200      1     6
City_1      200      2     6
City_1      800      3     7
City_1      800      8     0"""

df1 = pd.DataFrame([x.split() for x in df1_string.split('\n')], columns=['City', 'Pop', 'Homes', 'Other'])
df2 = pd.DataFrame([x.split() for x in df2_string.split('\n')], columns=['City', 'Pop', 'Homes', 'Other'])

df1_keys = [x for x in df1.groupby(['Pop', 'Homes']).groups.keys()]
df2_keys = [x for x in df2.groupby(['Pop', 'Homes']).groups.keys()]

print(df1_keys)
[('100', '1'), ('100', '2'), ('100', '3'), ('200', '1'), ('200', '2'), ('200', '3'), ('300', '1')]
print(df2_keys)
[('100', '1'), ('100', '2'), ('100', '8'), ('200', '1'), ('200', '2'), ('800', '3'), ('800', '8')]

但是我不知道如何从这里过滤df1。我以为会是这样:

df1 = df1[df1.groupby(['Pop', 'Homes']).groups.keys().isin(df2.groupby(['Pop', 'Homes']).groups.keys())]   

但这不起作用。

我还应该提到,df1和df2的长度并不总是相同。

解决方案

df1.set_index(['Pop', 'Homes'], inplace=True)
df2.set_index(['Pop', 'Homes'], inplace=True)

df1 = df1[df2.index.isin(df1.index)]

df1.reset_index(inplace=True)

1 个答案:

答案 0 :(得分:0)

将索引设置为Pop和Home会生成值“ pairs”,并使用isin()应用所需的过滤器:

df1.set_index(['Pop', 'Homes'], inplace=True)
df2.set_index(['Pop', 'Homes'], inplace=True)

df1 = df1[df2.index.isin(df1.index)]

df1.reset_index(inplace=True)
print(df1)