Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a DataFrame:

import pandas as pd

import numpy as np

df = pd.DataFrame({'foo.aa': [1, 2.1, np.nan, 4.7, 5.6, 6.8],

                   'foo.fighters': [0, 1, np.nan, 0, 0, 0],

                   'foo.bars': [0, 0, 0, 0, 0, 1],

                   'bar.baz': [5, 5, 6, 5, 5.6, 6.8],

                   'foo.fox': [2, 4, 1, 0, 0, 5],

                   'nas.foo': ['NA', 0, 1, 0, 0, 0],

                   'foo.manchu': ['NA', 0, 0, 0, 0, 0],})

I want to select values of 1 in columns starting with foo.. Is there a better way to do it other than:

df2 = df[(df['foo.aa'] == 1)|

(df['foo.fighters'] == 1)|

(df['foo.bars'] == 1)|

(df['foo.fox'] == 1)|

(df['foo.manchu'] == 1)

]

Something similar to writing something like:

df2= df[df.STARTS_WITH_FOO == 1]

The answer should print out a DataFrame like this:

   bar.baz  foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu nas.foo

0      5.0     1.0         0             0        2         NA      NA

1      5.0     2.1         0             1        4          0       0

2      6.0     NaN         0           NaN        1          0       1

5      6.8     6.8         1             0        5          0       0

[4 rows x 7 columns]

1 Answer

0 votes
by (41.4k points)
edited by

To create your columns perform a list comprehension :

In [28]:

filter_col = [col for col in df if col.startswith('foo')]

filter_col

Out[28]:

['foo.aa', 'foo.bars', 'foo.fighters', 'foo.fox', 'foo.manchu']

In [29]:

df[filter_col]

Out[29]:

   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu

0     1.0         0             0        2         NA

1     2.1         0             1        4          0

2     NaN         0           NaN        1          0

3     4.7         0             0        0          0

4     5.6         0             0        0          0

5     6.8         1             0        5          0

If you want to make your career in Artificial Intelligence then go through this video:

Browse Categories

...