DataCapitalist
DataCapitalist

Reputation: 55

Python DataFrame layout change

I have data in a pandas DataFrame as shown in Format 1 in the attached image.

I would like to change the layout to Format 2 as specified.

My first thought was to use the pandas pivot_table function but I realize it wouldn't work here because it requires you to specify an aggfunc. In the case of my request, I do not want to aggregate. Instead I want discrete values arranged similar to Format 2.

Any suggestions?

enter image description here

Thanks!

Upvotes: 0

Views: 156

Answers (1)

G. Anderson
G. Anderson

Reputation: 5955

As commented, this is a simple pivot

df1=pd.DataFrame({'ID':[123,123,456,456],'Phase':['a','b','a','b'],'Date':['9/3','11/5','6/3','7/5']})

df1.pivot(index='ID',columns='Phase')

    Date
Phase   a   b
ID      
123 9/3 11/5
456 6/3 7/5

Upvotes: 1

Related Questions