So I have this DF:
In [130]: dfAbr
Out[130]:
ip ospfArea router_name
0 1.1.1.1 0.0.0.2 Router1-1
1 1.1.1.2 0.0.0.2 Router1-2
140 5.5.5.1 0.0.0.5 Router5-1
141 5.5.5.2 0.0.0.5 Router5-2
I'd like to reshape that into a 2-row dataframe, such as:
I've been playing with both stack/unstack
and pivot
functions but couldn't go that far.
For example, dfAbr1 = pd.DataFrame(dfAbr.set_index('ospfArea').stack()).reset_index()
, renaming column names, produces the following:
In [151]: dfAbr1
Out[151]:
ospfArea level1 level2
0 0.0.0.2 ip 1.1.1.1
1 0.0.0.2 router_name Router1-1
2 0.0.0.2 ip 1.1.1.2
3 0.0.0.2 router_name Router1-2
4 0.0.0.5 ip 5.5.5.1
5 0.0.0.5 router_name Router5-1
6 0.0.0.5 ip 5.5.5.2
7 0.0.0.5 router_name Router5-2
From there, I wanted to pivot
it, like this: dfAbr1.pivot(index='ospfArea', columns='level1',values='level2')
, but couln't do it as I get Index contains duplicate entries, cannot reshape
. I believe this is because under level1
I have duplicate values...
Any other way of doing this?
Thanks!