pandas 索引查看与复制 [英] Pandas Indexing-View-Versus-Copy

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

问题描述

我有一个包含几列的数据框。
之后,添加了标题为有效的列。
如果音量列中的值大于0,则需要将有效设置为1。

I have a dataframe with several columns. Later, a column titled 'Active' is added. If the 'Volume' column contains anything greater than 0, I need to set 'Active' to 1.

这是一个简单示例,说明了 ve尝试过:

This is a simple example of how I've attempted it:

import pandas as pd

active_df = pd.DataFrame(columns=['Volume'])
active_df['Volume'] = 0, 0, 22, 22, 0, 22, 0, 22, 0, 22
active_df['Active'] = 0

active_df['Active'].loc[active_df['Volume'] > 0] = 1

print(active_df)

尽管这会产生预期的结果结果,我不断收到警告:试图在DataFrame的切片副本上设置一个值

Although this produces the expected results, I constantly get a warning: "A value is trying to be set on a copy of a slice from a DataFrame"

我已经阅读了引用的页面: http://pandas.pydata.org/pandas- docs / stable / indexing.html#indexing-view-versus-copy ,但仍然无法解决。

I have read the referenced page: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy but still can't solve this.

我以为我已经处理了这个问题

I thought that I had dealt with this in other code and resolved it, but I can't find an example in existing code.

推荐答案

我在其他代码中解决了该问题,但在现有代码中找不到示例。

I rediscovered this question after it being up for a year after a recent upvote. Having learned a lot more about Pandas since it was asked, I thought I'd revisit the difference in my 'copy of a slice' and the solution.

我最初的尝试是:

active_df['Active'].loc[active_df['Volume'] > 0] = 1

这充其量只是一个令人费解的方法。

Which was really a convoluted way at best.

首先,我获取active_df ['Volume']> 0的布尔值,然后在行值为TRUE的情况下,将切片active_df ['Active']设置为1 。
尽管这可行,但不确定是数据框的视图还是副本。

First I'm gettting boolean values for active_df['Volume'] > 0 And then where the row value is TRUE, I'm setting the slice active_df['Active'] to 1. Although this worked, there was uncertainty in whether this was a view or copy of the dataframe.

解决方案是:

active_df.loc[active_df['Volume'] > 0, 'Active'] = 1

在active_df数据框中,找到active_df ['Volume ']> 0,然后在 Active列中设置这些值,并将其设置为1。

In the active_df dataframe, locate the rows where active_df['Volume'] > 0, and the column 'Active', and set those values to 1.

或者用另一种方式表示:为 Active设置值1

Or stated a different way: Set a value of 1 for the 'Active' column for the rows that have a value of 0 in the 'Volume' column.

所以您实际上是在整个数据帧(active_df.loc)而不是切片上工作以及可能的副本(active_df ['Active']。loc)

So you are really working on the whole dataframe (active_df.loc) instead of the slice and possible copy (active_df['Active'].loc)

再次感谢@Deena提供解决方案。

Thank you again to @Deena for providing the solution.

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

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