2

I have an input dataframe df, where I want to transform column A...

A   B
    asd
0   dsf
1   ewr
2   dfds
3   sdf

...into my expected output df1:

        Aa  Ab  Ac              Bb
                                asd
        0   0   0               dsf
        1   0   0               ewr
        1   1   0               dfds
        1   1   1               sdf

Code:

if df['A'] == '' :
    df1['Aa'] = ''
elif df['A'] == 1 :
    df1['Aa'] == 1
elif df['A'] == 0 :
    df1['Aa'] == 0
else:
    df1['Aa'] == 1

Error:

if df['A'] == '' :                                                                                                                                                       
  File "C:\Python\Python38\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__                                                                                              
    raise ValueError(                                                                                                                                                                        
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Similar formula in excel

 B2 =IF(A2=1,1,IF(A2="","",IF(A2=0,0,1)))

Similarly;

 C2 = IF(A2=2,1,IF(A2="","",IF(A2=0,0,1)))

Trying to achieve something similar with python. Please Help!!

Logic :

if value in df['A'] == ''; new column 'Aa,Ab,Ac' in df1 == ''
if value in df['A'] == 1; new column 'Aa' in df1 == 1, and rest (Ab,Ac) == 0
if value in df['A'] == 2; new column 'Aa, Ab' in df1== 1, and rest (Ac) == 0
if value in df['A'] == 3; new column 'Aa,Ab,Ac' in df1 == 1

So, the flow can be something like:

df1:

A   Aa  Ab  Ac              Bb
                            asd
0   0   0   0               dsf
1   1   0   0               ewr
2   1   1   0               dfds
3   1   1   1               sdf

Later just remove column A. So Final df1

        Aa  Ab  Ac              Bb
                                asd
        0   0   0               dsf
        1   0   0               ewr
        1   1   0               dfds
        1   1   1               sdf
5
  • Do the values in Aa, Ab, Ac actually depend on the original values in A beyond carrying the empty string value across the whole row? Or do you just want 1s and 0s arranged in this triangular pattern? Commented Oct 6, 2020 at 19:48
  • Have a look into the numpy.where() function. Here are the docs. It adds excellent 'if/then/else' logic to DataFrames. Commented Oct 6, 2020 at 20:30
  • Your input column df.A is numeric, so trying to do a string compare df['A'] == '' will also fail for that reason, as well as the issue you get with the output being a series not just a scalar. Commented Oct 6, 2020 at 21:42
  • Essentially you want to convert the 2-bit binary value in df.A into binary-encoded columns [Aa,Ab,Ac]. Although they're flipped left-to-right; normally encoding would start from the LSB, i.e. Ac. Commented Oct 6, 2020 at 21:45
  • @G.Anderson They do depend on A, this is just an example. In my dataset I have multiple 1s,2s and so on... based on another logic so A could be like 0 0 0 0 1 1 3 2 2... . I have provided the logic that I need for calculation of Aa Ab Ac{random column names}, just can't determine how to code this Commented Oct 7, 2020 at 5:56

2 Answers 2

3

Found a way to do this, just put this inside a function, and you could use it for the other nested loops too

   conditions = [
        (df['A'] == 0),
        (df['A'] == 1),
        (df['A'] > 1)]
    choices = [0, 1, 1]
    df1['Aa'] = np.select(conditions, choices, default='null')
Sign up to request clarification or add additional context in comments.

Comments

1

IIUC, try:

import pandas as pd    
df = pd.DataFrame({'a':["",0,1,2,3]})
df['new_a'] = pd.to_numeric(df['a'], errors='coerce')
df['Aa'] = df['new_a'].apply(lambda x: np.nan if math.isnan(x) else (0 if x==0 else (OTHER CONDITION)))

you can use the similar logic to create your 'Ab', 'Ac', and 'Bb'

4 Comments

Oddly I am facing the same issue today, I have many nested loops so the solution proposed wont work....am not sure why this is happening
Similar situation as that of @El_1988, not sure this solution works for me
@RoshanShah22 I posted my question here: stackoverflow.com/questions/64237931/… , if a solution shows up will share here
Hey @El_1988, I tried something and it seems to work. Maybe give it a shot

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.