Pandas DataFrame过滤列A,具体取决于列B是否包含x作为A中的值组

时间:2018-08-14 21:42:33

标签: python pandas dataframe

我想根据import imageio import os path = '/Users/myusername/Desktop/Pics/' # on Mac: right click on a folder, hold down option, and click "copy as pathname" image_folder = os.fsencode(path) filenames = [] for file in os.listdir(image_folder): filename = os.fsdecode(file) if filename.endswith( ('.jpeg', '.png', '.gif') ): filenames.append(filename) filenames.sort() # this iteration technique has no built in order, so sort the frames images = list(map(lambda filename: imageio.imread(filename), filenames)) imageio.mimsave(os.path.join('movie.gif'), images, duration = 0.04) # modify duration as needed 中的值,列df是否包含值{{1},来过滤ref列下面的DataFrame ref }。

type

在此,'P'的值1、3和4在列In [32]: df Out[32]: ref type 0 1 P 1 1 C 2 1 A 3 2 C 4 3 P 5 3 P 6 4 P 7 4 A 8 5 C 9 5 A 中包含至少一行,值ref,而2和5则不行

我试图用'P' 2和5过滤掉任何行,以便最终输出为:

type

我该怎么做(最好一步一步)?

1 个答案:

答案 0 :(得分:2)

使用groupbyfilter

df.groupby('ref').filter(lambda x : ('P' in x['type'].values))

返回:

   ref type
0    1    P
1    1    C
2    1    A
4    3    P
5    3    P
6    4    P
7    4    A