I have a data frame where one column is categorical strings and the next one is the values corresponding to it:
df = pd.DataFrame(list((['a', 'b', 'c', 'buy', 5],
['f', 'b', 'a', 'buy', 2],
['a', 'b', 'c', 'sold', 6],
['a', 'b', 'f', 'buy', 4],
['a', 'b', 'c', 'returned', 'yes'])), columns = ['attr1', 'attr2','attr3','status','value'])
I want to create new columns based on df.status
column, and fill empty ones with np.nan
, requires pivot on multiple columns:
I am looking for an efficient solution that works for large data frames.
question from:
https://stackoverflow.com/questions/65873915/extract-new-columns-and-fill-values-based-on-categorical-values-data-frame-in-py 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…