pandas Data Analysis - DataFrame column

People often want a DataFrame or more columns as the row index to use, or may want to row index as DataFrame columns.

frame1 = DataFrame({'a':range(7),'b':range(7,0,-1),
'c':['one','one','one','two','two','two','two'],
'd':[0,1,2,0,1,2,3]})
print(frame1)

DataFrame of set_index function will be one or more columns into rows indexed and create a new DataFrame.

print(frame1.set_index('c','d'))

By default, those listed in DataFrame will be removed, but it can also be preserved.

print(frame1.set_index(['c','d'],drop=False))

reset_index function just the opposite with set_index, hierarchical level of the index will be transferred to the inside.

frame2 = frame1.set_index(['c','d'])
print(frame2.reset_index())

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/li98/p/10993510.html