只需致电
reset_index():
In [130]: sOut[130]:011999-03-31 SOLD_PRICE NaN1999-06-30 SOLD_PRICE NaN1999-09-30 SOLD_PRICE NaN1999-12-31 SOLD_PRICE 32000-03-31 SOLD_PRICE 3Name: 2, dtype: float64In [131]: s.reset_index()Out[131]: 01 20 1999-03-31 SOLD_PRICE NaN1 1999-06-30 SOLD_PRICE NaN2 1999-09-30 SOLD_PRICE NaN3 1999-12-31 SOLD_PRICE 34 2000-03-31 SOLD_PRICE 3
有多种删除列的方法:
调用
reset_index()两次并指定一列:
In [136]: s.reset_index(0).reset_index(drop=True)Out[136]: 0 20 1999-03-31 NaN1 1999-06-30 NaN2 1999-09-30 NaN3 1999-12-31 34 2000-03-31 3
重置索引后删除列:
In [137]: df = s.reset_index()In [138]: dfOut[138]: 01 20 1999-03-31 SOLD_PRICE NaN1 1999-06-30 SOLD_PRICE NaN2 1999-09-30 SOLD_PRICE NaN3 1999-12-31 SOLD_PRICE 34 2000-03-31 SOLD_PRICE 3In [139]: del df[1]In [140]: dfOut[140]: 0 20 1999-03-31 NaN1 1999-06-30 NaN2 1999-09-30 NaN3 1999-12-31 34 2000-03-31 3
drop()重置后致电:
In [144]: s.reset_index().drop(1, axis=1)Out[144]: 0 20 1999-03-31 NaN1 1999-06-30 NaN2 1999-09-30 NaN3 1999-12-31 34 2000-03-31 3
然后,在重置索引后,只需重命名列
In [146]: df.columns = ['Date', 'Sales']In [147]: dfOut[147]: Date Sales0 1999-03-31 NaN1 1999-06-30 NaN2 1999-09-30 NaN3 1999-12-31 34 2000-03-31 3
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)