python输入错误怎么删除,Python'reset_index(drop = True)'函数错误地删除了列

I have a Pandas dataframe named data_match. It contains columns '_worker_id', '_unit_id', and 'caption'. (Please see attached screenshot for some of the rows in this dataframe)

6e57efb2b1f1fff36bc3078cf7c4caf6.png

Let's say the index column is not in ascending order (I want the index to be 0, 1, 2, 3, 4...n) and I want it to be in ascending order. So I ran the following function attempting to reset the index column:

data_match=data_match.reset_index(drop=True)

I was able to get the function to return the correct output in my computer using Python 3.6. However, when my coworker ran that function in his computer using Python 3.6, the '_worker_id' column got removed.

Is this due to the '(drop=True)' clause next to 'reset_index'? But I didn't know why it worked in my computer and not in my coworker's computer. Can anybody advise?

解决方案

As the saying goes, "What happens in your interpreter stays in your

interpreter". It's impossible to explain the discrepancy without seeing the

full history of commands entered into both Python interactive sessions.

However, it is possible to venture a guess:

df.reset_index(drop=True)

drops the current index of the DataFrame and replaces it with an index of

increasing integers. It never drops columns.

So, in your interactive session, _worker_id was a column. In your co-worker's

interactive session, _worker_id must have been an index level.

The visual difference can be somewhat subtle. For example, below, df has a

_worker_id column while df2 has a _worker_id index level:

In [190]: df = pd.DataFrame({'foo':[1,2,3], '_worker_id':list('ABC')}); df

Out[190]:

_worker_id foo

0 A 1

1 B 2

2 C 3

In [191]: df2 = df.set_index('_worker_id', append=True); df2

Out[191]:

foo

_worker_id

0 A 1

1 B 2

2 C 3

Notice that the name _worker_id appears one line below foo when it is an

index level, and on the same line as foo when it is a column. That is the only

visual clue you get when looking at the str or repr of a DataFrame.

So to repeat: When _worker_index is a column, the column is unaffected by

df.reset_index(drop=True):

In [194]: df.reset_index(drop=True)

Out[194]:

_worker_id foo

0 A 1

1 B 2

2 C 3

But _worker_index is dropped when it is part of the index:

In [195]: df2.reset_index(drop=True)

Out[195]:

foo

0 1

1 2

2 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值