重置列索引 pandas ? [英] Reset Column Index Pandas?

查看:67
本文介绍了重置列索引 pandas ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>>>>data = data.drop(data.columns[[1,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]],axis=1)>>>数据 = data.drop(data.index[[0,1]],axis = 0)>>>打印(数据.头())0 2 3 4 202 500292014600 .00 .00 .00 NaN3 500292014600 100.00 .00 .00 NaN4 500292014600 11202.00 .00 .00 NaN>>>数据 = data.reset_index(drop = True)>>>打印(数据.头())0 2 3 4 200 500292014600 .00 .00 .00 NaN1 500292014600 100.00 .00 .00 NaN2 500292014600 11202.00 .00 .00 NaN

为什么当我使用 df.reset_index 时,我的列的索引没有被重置?如何将此索引重置为 0、1、2、3、4?

解决方案

尝试替换列名:

<预><代码>>>>将 numpy 导入为 np>>>将熊猫导入为 pd>>>my_data = [[500292014600, .00, .00, .00, np.nan],[500292014600, 100.00, .00, .00, np.nan],[500292014600, 11202.00, .00, .00, np.nan]]>>>df = pd.DataFrame(my_data, columns=[0,2,3,4,20])>>>df0 2 3 4 200 500292014600 0.0 0.0 0.0 NaN1 500292014600 100.0 0.0 0.0 NaN2 500292014600 11202.0 0.0 0.0 NaN>>>df.columns = 范围(df.shape[1])>>>df0 1 2 3 40 500292014600 0.0 0.0 0.0 NaN1 500292014600 100.0 0.0 0.0 NaN2 500292014600 11202.0 0.0 0.0 NaN

>>> data = data.drop(data.columns[[1,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]], axis=1)
>>> data = data.drop(data.index[[0,1]],axis = 0)
>>> print(data.head())
             0         2    3    4    20
2  500292014600       .00  .00  .00  NaN
3  500292014600    100.00  .00  .00  NaN
4  500292014600  11202.00  .00  .00  NaN
>>> data = data.reset_index(drop = True)
>>> print(data.head())
              0         2    3    4    20
 0  500292014600       .00  .00  .00  NaN
 1  500292014600    100.00  .00  .00  NaN
 2  500292014600  11202.00  .00  .00  NaN

How come when i use df.reset_index the index of my columns is not reset? How do I go about resetting this index to 0,1,2,3,4?

解决方案

Try replacing the column names:

>>> import numpy as np
>>> import pandas as pd

>>> my_data = [[500292014600, .00, .00, .00, np.nan],
              [500292014600, 100.00, .00, .00, np.nan], 
              [500292014600, 11202.00, .00, .00, np.nan]]
>>> df = pd.DataFrame(my_data, columns=[0,2,3,4,20])
>>> df
              0        2    3    4  20
0  500292014600      0.0  0.0  0.0 NaN
1  500292014600    100.0  0.0  0.0 NaN
2  500292014600  11202.0  0.0  0.0 NaN

>>> df.columns = range(df.shape[1])
>>> df
              0        1    2    3   4
0  500292014600      0.0  0.0  0.0 NaN
1  500292014600    100.0  0.0  0.0 NaN
2  500292014600  11202.0  0.0  0.0 NaN

这篇关于重置列索引 pandas ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆