熊猫:选择数据框匹配和不匹配条件

时间:2020-11-07 14:02:03

标签: python pandas dataframe

我有两个DF,如下:

ID | A | B
1    0   1
2    1   2
3    2   2

Type | Name
1    |  ...
2    |  ...
1    |  ...

我想要第一个中的两个DataFrame。第一个仅包含SECOND DF中具有Type == 1的行,另一个包含第二个DF Type != 1中的行。这是我的示例期望结果:

First DF:
ID | A | B
1    0   1
3    2   2

Second DF:
ID | A | B
2    1   2

我们叫第一个df和第二个df_other。我试过了,但这给了我错误

idx = df_other["Type"] == 1
df1 = df[~df.index.isin(idx)]
df2 = df[df.index.isin(idx)]

我也尝试了loc[idx].iloc[idx, :],但它们也给出了错误的结果。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

如果行数相同,并且每个数据框的排序正确,请合并/合并它们以进行过滤:

data = {
        'ID':[1,2,3],
        'A':[0,1,2],
        'B':[1,2,2],
        }
dfa = pd.DataFrame(data)

data1 = {
        'Type':[1,2,1],
        'Name':['---','---','---']
        }
dfb = pd.DataFrame(data1)


df_merged = pd.concat([dfa, dfb], axis=1)
df1 = df_merged.loc[df_merged['Type']==1][['ID', 'A', 'B']].set_index('ID')
df2 = df_merged.loc[df_merged['Type']!=1][['ID', 'A', 'B']].set_index('ID')
print(df1)
print(df2)

答案 1 :(得分:0)

让我们将数据帧称为df1df2。我假设两者的长度相同。

import numpy as np
import pandas as pd

mask1 = np.select([df2['Type']==1, [True], default=False) #this creates a pandas series with True values were df2['Type'] is 1 and False everywhere else.

#### Your first dataframe would be
df1[mask1]
#### Your second dataframe is
df1[~mask1]

# if you want to save them as dataframes best use .copy() i.e.:
df_type1 = df1[mask1].copy()
df_typeNot1 = df1[~mask1].copy()