2

I have the following table loaded as a dataframe in Python using pandas

+--------+-------+------+
| Number | Col1  | Col2 |
+--------+-------+------+
| ABC    | TRUE  | SFG  |
| BCD    | TRUE  |      |
| CDE    | FALSE | SFG  |
| DEF    | FALSE |      |
| FEG    | TRUE  | JJI  |
+--------+-------+------+

Number, Col2 - String; Col1 - Boolean

I want to select rows from this df using the following logic

IF Col1 = TRUE and Col2 is not null Select Number + "," + Col2
ELSE IF Col1 = TRUE and Col2 is null Select Number
ELSE IF Col2 is not null and Col1 = FALSE Select Col2

In the above case, the output should be a list with the following values

["ABC", "SFG", "BCD", "FEG", "JJI"] //Removing the repetition too ("SFG")

How do I go about implementing this logic in Python using Pandas?

2
  • hmmm, i got ['ABC,SFG', 'BCD', 'SFG', nan, 'FEG,JJI'] following your logic Commented Oct 20, 2016 at 21:07
  • 1
    you'll probably want to use nested numpy.where statements. Commented Oct 20, 2016 at 21:18

2 Answers 2

2

use where + stack + tolist

pd.concat([df.Number.where(df.Col1, np.nan), df.Col2], axis=1).stack().tolist()

['ABC', 'SFG', 'BCD', 'SFG', 'FEG', 'JJI']

to get a unique list

pd.concat([df.Number[df.Col1], df.Col2.dropna()]).unique().tolist()

['ABC', 'BCD', 'FEG', 'SFG', 'JJI']
Sign up to request clarification or add additional context in comments.

Comments

2

Here is an implementation of your query in multiple steps:

import pandas as pd
df = pd.DataFrame(data={'Number': ['ABC', 'BCD', 'CDE', 'DEF', 'FEG'],
                        'Col1': [True, True, False, False, True],
                        'Col2': ['SFG', None, 'SFG', None, 'JJI']})
cond1 = df.Col1 & ~df.Col2.isnull()
cond2 = df.Col1 & df.Col2.isnull()
cond3 = ~df.Col1 & ~df.Col2.isnull()
selects = [df[cond1].Number + ',' + df[cond1].Col2, 
           df[cond2].Number, 
           df[cond3].Col2]
result = pd.concat(selects).sort_index()

result is (the same as @MaxU predicted)

0    ABC,SFG
1        BCD
2        SFG
4    FEG,JJI
dtype: object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.