我有一个输入数据框df,我想在其中转换列A...
A B
asd
0 dsf
1 ewr
2 dfds
3 sdf
...进入我的预期输出df1:
Aa Ab Ac Bb
asd
0 0 0 dsf
1 0 0 ewr
1 1 0 dfds
1 1 1 sdf
代码:
if df['A'] == '' :
df1['Aa'] = ''
elif df['A'] == 1 :
df1['Aa'] == 1
elif df['A'] == 0 :
df1['Aa'] == 0
else:
df1['Aa'] == 1
错误:
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().
excel中类似的公式
B2 =IF(A2=1,1,IF(A2="","",IF(A2=0,0,1)))
相似地;
C2 = IF(A2=2,1,IF(A2="","",IF(A2=0,0,1)))
尝试用 python 实现类似的东西。请帮忙!!
逻辑:
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
因此,流程可能类似于:
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
稍后只需删除列即可A。所以Final df1
Aa Ab Ac Bb
asd
0 0 0 dsf
1 0 0 ewr
1 1 0 dfds
1 1 1 sdf
守着一只汪
慕斯709654
相关分类