I would like to add new column to a DataFrame based on a condition.
df = pd.DataFrame({'category' : ['A', 'A', 'B', 'C'],
'value': [10,10,20,20]
})
On DataFrame above, I would like to add separated columns ['Value A'], ['Value B'], ['Value C'] to make calculations easier. Desired output would be like
df = pd.DataFrame({'category' : ['A', 'A', 'B', 'C'],
'value': [10,10,20,20]
'Value A' : [10, 10, 0, 0]
'Value B' : [0, 0, 20, 0]
'Value C' : [0, 0, 0, 20]
})
I thought I could just do it by
df['Value A'] = df['value'](add some condition here).fillna(0)
But seems like it can be done this simple. Is there any way to do that?