0

I am trying to replace all values in a pandas dataframe column df.column_A if they fall within the range of 1 to 10.

However, when I do:

df.loc[(1 < df.column_A < 10), "Column_A"] = 1

Produces:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Alternatively, when I do:

df.loc[(df.column_A < 10) & (df.column_A > 1), "df.column_A"] = 1

I am not getting an error at all, but the values don't get replaced.

What is strange is that when I do:

df.loc[(df.column_A < 10) | (df.column_A > 1), "df.column_A"] = 1

all values in df.column_A get replaced with 1, as I would expect.

This means that the syntax of the line is correct, so the mistake must be due to some factors I don't understand.

What am I doing wrong?

4

1 回答 1

1

这是一个简单的问题。.loc采用索引标签或布尔列表/系列。所以这将起作用:

df.loc[(df.column_A < 10) & (df.column_A > 1), "column_A"] = 1

请注意,我df.从列索引位置删除。


df.loc[(1 < df.column_A < 10), "Column_A"] = 1

将不起作用,因为该操作(1 < df.column_A < 10)看起来合乎逻辑,但会尝试将整个 Series 折叠为一个值。而且由于它不知道您是否想要一个或其他组合,它会引发该错误andor

df.loc[(df.column_A < 10) | (df.column_A > 1), "df.column_A"] = 1

也不应该工作,因为您没有正确引用列。有趣的是,您没有收到任何错误。也许你之前在你的程序中做了一些可以节省你的事情......

于 2016-11-18T04:32:13.130 回答