reset_index()函数

 

1 重置索引后,drop参数默认为False,想要删除原先的索引列要置为True.想要在原数据上修改要inplace=True.特别是不赋值的情况必须要加,否则drop无效.

all_user_repay.reset_index(drop=True,inplace=True)

 

df1 = pd.DataFrame({'A': ['A0', 'A1'],
                    'B': ['B0', 'B1'],
                    'C': ['C0', 'C1'],
                    'D': ['D0', 'D1']})
df2 = pd.DataFrame({'A': ['A4', 'A5'],
                    'B': ['B4', 'B5'],
                    'C': ['C4', 'C5'],
                    'D': ['D4', 'D5']})
frames = [df1, df2]
result = pd.concat(frames)
print(result.reset_index())
#    index   A   B   C   D
# 0      0  A0  B0  C0  D0
# 1      1  A1  B1  C1  D1
# 2      0  A4  B4  C4  D4
# 3      1  A5  B5  C5  D5
print(result.reset_index(drop=True))
#     A   B   C   D
# 0  A0  B0  C0  D0
# 1  A1  B1  C1  D1
# 2  A4  B4  C4  D4
# 3  A5  B5  C5  D5

 Series.reset_index()

注意参数level默认移除原先的全部索引,即将原先的全部索引都置为普通列.

如果给level赋值,则只有所赋值的索引列置为普通列,其余的留下做索引列.

参考:http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.reset_index.html?highlight=reset_index#pandas.Series.reset_index

arrays = [np.array(['bar', 'bar', 'baz', 'baz']),
         np.array(['one', 'two', 'one', 'two'])]
s2 = pd.Series(
               range(4), name='foo',
               index=pd.MultiIndex.from_arrays(arrays,
               names=['a', 'b']))
print(s2)
#这里如果想要保留修改不能用inplace参数,只能再赋给另一个引用
print(s2.reset_index(level='a'))
print(s2.reset_index())
print(type(s2))
# a    b
# bar  one    0
#      two    1
# baz  one    2
#      two    3
# Name: foo, dtype: int64
#        a  foo
# b
# one  bar    0
# two  bar    1
# one  baz    2
# two  baz    3
#      a    b  foo
# 0  bar  one    0
# 1  bar  two    1
# 2  baz  one    2
# 3  baz  two    3
# <class 'pandas.core.series.Series'>

 

 

 

 

 

 

2 把某一列设为索引列

df.set_index('列名',inplace=True)

转载于:https://www.cnblogs.com/xxswkl/p/10997437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值