看看pandas系列每一行中的项目

时间:2017-08-18 07:05:50

标签: python pandas

我有一个包含以下数据的熊猫系列:

2015-07-24                            ['Business', 'Corporate']
2015-07-24                            ['Business', 'Corporate']
2015-07-08                              ['Commentary', 'World']
2015-07-05                          ['Issues', 'JUST BE CAUSE']
2015-06-15                                         ['Business']
2015-04-11                              ['Business', 'Economy']
2015-03-24                                     ['Food & Drink']
2015-02-24                              ['Commentary', 'Japan']
2015-02-19                    ['Business', 'Financial Markets']
2015-01-05     ['National', '70 YEARS OF PEACE AND PROSPERITY']
2014-11-06                                         ['National']
2014-10-31                    ['Business', 'Financial Markets']
2014-10-20                                         ['Business']
2014-09-22                              ['Business', 'Economy']
2014-09-02                            ['Business', 'Corporate']
2014-08-08                    ['Business', 'Financial Markets']
2014-07-18    ['Business', 'Financial Markets', 'TSE DATA & ...
2014-07-15                              ['Business', 'Economy']
2014-07-10                                         ['National']
2013-12-16                    ['Business', 'Financial Markets']
2013-10-29                                         ['National']
2013-10-15                                         ['National']
2013-10-06                    ['Business', 'Financial Markets']
2013-06-25                    ['Business', 'Financial Markets']
2013-06-17                                       ['Editorials']
2013-05-21                  ['Voices', 'VIEWS FROM THE STREET']
2013-05-15                    ['Business', 'Financial Markets']
2013-05-03                                         ['National']
2013-03-07                    ['Business', 'Financial Markets']
2013-02-10                              ['Business', 'Economy']
Name: tags, Length: 216, dtype: object

有没有办法得到一个布尔数组告诉我每个列表是否包含“Business”或“Food& Drink”?

2 个答案:

答案 0 :(得分:2)

如果值为list s,请使用:

print (df)
                                        tags
2015-07-24          ['Business','Corporate']
2015-07-24       ['Business','Food & Drink']
2015-07-08            ['Commentary','World']
2015-07-05        ['Issues','JUST BE CAUSE']
2015-06-15                      ['Business']
2015-04-11  ['Business','Food & Drink', 'a']

val = set(['Business','Food & Drink'])

m = df['tags'].apply(lambda x: set(x) >= val)
print (m)
2015-07-24    False
2015-07-24     True
2015-07-08    False
2015-07-05    False
2015-06-15    False
2015-04-11     True
Name: tags, dtype: bool

答案 1 :(得分:1)

使用set

df.tags.apply(set) & set(['Business', 'Food & Drink'])

2015-07-24     True
2015-07-24     True
2015-07-08    False
2015-07-05    False
2015-06-15     True
2015-04-11     True
2015-03-24     True
2015-02-24    False
2015-02-19     True
2015-01-05    False
2014-11-06    False
2014-10-31     True
2014-10-20     True
2014-09-22     True
2014-09-02     True
2014-08-08     True
2014-07-18     True
2014-07-15     True
2014-07-10    False
2013-12-16     True
2013-10-29    False
2013-10-15    False
2013-10-06     True
2013-06-25     True
2013-06-17    False
2013-05-21    False
2013-05-15     True
2013-05-03    False
2013-03-07     True
2013-02-10     True
Name: tags, dtype: bool

并排查看结果

df.assign(i=df.tags.apply(set) & set(['Business', 'Food & Drink']))

                                                    tags      i
2015-07-24                         [Business, Corporate]   True
2015-07-24                         [Business, Corporate]   True
2015-07-08                           [Commentary, World]  False
2015-07-05                       [Issues, JUST BE CAUSE]  False
2015-06-15                                    [Business]   True
2015-04-11                           [Business, Economy]   True
2015-03-24                                [Food & Drink]   True
2015-02-24                           [Commentary, Japan]  False
2015-02-19                 [Business, Financial Markets]   True
2015-01-05  [National, 70 YEARS OF PEACE AND PROSPERITY]  False
2014-11-06                                    [National]  False
2014-10-31                 [Business, Financial Markets]   True
2014-10-20                                    [Business]   True
2014-09-22                           [Business, Economy]   True
2014-09-02                         [Business, Corporate]   True
2014-08-08                 [Business, Financial Markets]   True
2014-07-18     [Business, Financial Markets, TSE DATA &]   True
2014-07-15                           [Business, Economy]   True
2014-07-10                                    [National]  False
2013-12-16                 [Business, Financial Markets]   True
2013-10-29                                    [National]  False
2013-10-15                                    [National]  False
2013-10-06                 [Business, Financial Markets]   True
2013-06-25                 [Business, Financial Markets]   True
2013-06-17                                  [Editorials]  False
2013-05-21               [Voices, VIEWS FROM THE STREET]  False
2013-05-15                 [Business, Financial Markets]   True
2013-05-03                                    [National]  False
2013-03-07                 [Business, Financial Markets]   True
2013-02-10                           [Business, Economy]   True