简体   繁体   English

为什么这个 Python 熊猫代码不能在我的数据集上运行?

[英]Why doesn't this Python pandas code work on my dataset?

I am a newbie in data science, and I encountered a problem about pandas in Python.我是数据科学的新手,在 Python 中遇到了关于 pandas 的问题。 Basically, I want to substitute the value lower than 0 in a column with 0, and I wonder why this does not work:基本上,我想用 0 替换列中低于 0 的值,我想知道为什么这不起作用:

Image of my dataset: dataset:我的数据集的图像:数据集:
数据集

Original:原来的:

submit[submit.score<0].score = 0

Fixed:固定的:

submit.loc[submit.score<0, 'score'] = 0

I have already solved this problem by using iloc, but it really confuses me.我已经通过使用 iloc 解决了这个问题,但这真的让我很困惑。 Any explanation would be great.任何解释都会很棒。

Your first attempt is equivalent to submit[submit['score'] < 0]['score'] = 0 .您的第一次尝试相当于submit[submit['score'] < 0]['score'] = 0 Whenever you see multiple [ and ] pairs in your pandas code, it might be a bad sign.每当您在 pandas 代码中看到多个[]对时,这可能是一个不好的迹象。 In this case, with submit[submit['score'] < 0] you're creating a copy of your dataframe, so you're basically assigning 0 to the score column on that copy , which isn't going to do anything.在这种情况下,使用submit[submit['score'] < 0]您正在创建数据框的副本,因此您基本上将0分配给该副本上score列,这不会做任何事情。

By using loc , you eliminate the copy and assign directly to the dataframe.通过使用loc ,您可以消除副本并直接分配给数据框。

Using .loc is good, like the sibling answer says.使用.loc很好,就像兄弟回答说的那样。

Even better, sometimes, is to use chaining operations where you create new objects instead of mutating another in-place.有时,更好的是使用链接操作来创建新对象,而不是就地改变另一个对象。 This leads to code that is easy to read and follow.这导致代码易于阅读和遵循。

I would suggest the following:我建议如下:

submit = submit.assign(score=submit.score.clip(0, None))

It's still just one line, but it makes a new dataframe with the score column replaced.它仍然只是一行,但它创建了一个新的数据框,替换了score列。 The .clip() method is used to clamp the values into an interval, in this case so that anything less than 0 will be zero. .clip()方法用于将值钳制在一个区间内,在这种情况下,任何小于 0 的值都将为零。

This style makes it easy to add more operations in a chain (a style seen other places).这种风格可以很容易地在链中添加更多操作(在其他地方看到的风格)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM