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
numpy.where()function. Here are the docs. It adds excellent 'if/then/else' logic to DataFrames.df.Ais numeric, so trying to do a string comparedf['A'] == ''will also fail for that reason, as well as the issue you get with the output being a series not just a scalar.df.Ainto binary-encoded columns[Aa,Ab,Ac]. Although they're flipped left-to-right; normally encoding would start from the LSB, i.e.Ac.Acould be like0 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