Python'reset_index(drop = True)'函数错误地删除了列 [英] Python 'reset_index(drop=True)' function erroneously removed column

查看:2114
本文介绍了Python'reset_index(drop = True)'函数错误地删除了列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 data_match 的Pandas数据框。它包含列'_worker_id','_ unit_id'和'caption'。 (请参阅此数据框中某些行的附加屏幕截图)

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)

假设索引列不是按升序排列的(我希望索引为0,1,2,3,4 ... n),我希望它按升序排列。所以我运行以下函数尝试重置索引列:

data_match = data_match.reset_index(drop = True)

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)

我能够使用Python 3.6让函数在我的计算机中返回正确的输出。然而,当我的同事使用Python 3.6在他的计算机中运行该功能时,_ worker_id列被删除了。

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.

这是由于'(drop = reset_index 旁边的真实'条款?但我不知道为什么它在我的电脑上工作而不是在我的同事的电脑里。有人可以提供建议吗?

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?

推荐答案

俗话说你的口译员会在你的
翻译中留下什么。如果没有看到在两个Python交互式会话中输入的
命令的完整历史记录,就无法解释这种差异。

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)
删除DataFrame的当前索引,并用
的索引替换它整数。它从不丢弃列。

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.

因此,在您的交互式会话中, _worker_id 是一列。在您的同事的
交互式会话中, _worker_id 必须是索引级别。

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.

视觉差异可能有点微妙。例如,下面的 df 有一个
_worker_id 列,而 df2 _worker_id 指数级别:

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

请注意,名称 _worker_id 出现在 foo <下方一行当它是
索引级别时,并且当它是一列时与 foo 在同一行。这是在查看DataFrame的 str repr 时获得的唯一
视觉线索。

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.

重复一遍:当 _worker_index 是一列时,该列不受
<$ c $的影响c> df.reset_index(drop = True):

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

_worker_index 在它是索引的一部分时被删除:

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

这篇关于Python'reset_index(drop = True)'函数错误地删除了列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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