pandas 在循环python中更改数据框列中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50187932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:31:53  来源:igfitidea点击:

Changing value in data frame column in a loop python

pythonpandasdataframe

提问by HeadOverFeet

I am new to Python pandas library and using data frames. I am using Jupyter. I kind of lost with this syntax.

我是 Python pandas 库和使用数据框的新手。我正在使用 Jupyter。我有点迷恋这种语法。

I want to loop through rows and set new value to column new_value. I thought I would do it like this, but it raises an error.

我想遍历行并将新值设置为列new_value。我以为我会这样做,但它引发了错误。

df_merged['new_value'] = 0

for i, row in df_merged.iterrows():
    df_merged['new_value'][i] = i

I also tried to do a calculation like:

我还尝试进行如下计算:

 df_merged['new_value'][i] = df_merged['move_%'] * df_merged['value']

But it doesnt work.

但它不起作用。

I am getting this error:

我收到此错误:

/usr/lib/python3.4/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy after removing the cwd from sys.path.

/usr/lib/python3.4/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 试图在 DataFrame 切片的副本上设置一个值
请参阅文档中的警告:http://pandas。 pydata.org/pandas-文档/稳定/ indexing.html#索引视图抗复制从sys.path中除去后CWD。

What I am doing wrong here?
Thanks.

我在这里做错了什么?
谢谢。

采纳答案by Mihai Alexandru-Ionut

You can use just this:

你可以只使用这个:

df_merged['new_value'] = df.index

You can also use applymethod.

您也可以使用apply方法。

df_merged['new_value'] = df_merged.apply(lambda row : row.name, axis=1)

I am getting this error : A value is trying to be set on a copy of a slice from a DataFrame

我收到此错误:正在尝试在来自 DataFrame 的切片副本上设置值

It's not a error, it's just a warning message.

这不是错误,只是警告消息。

From thisanswer:

这个答案:

The SettingWithCopyWarningwas created to flag potentially confusing "chained" assignments, such as the following, which don't always work as expected, particularly when the first selection returns a copy.

SettingWithCopyWarning被创造的标志可能造成混淆的“链接”的任务,比如下面这并不总是如预期的工作,特别是当第一选择返回一个副本。

You can avoid this warning message using pd.DataFrame.locmethod.

您可以使用pd.DataFrame.loc方法避免此警告消息。

for i, row in df_merged.iterrows():
    df_merged.loc[i,'price_new']  = i 

回答by HeadOverFeet

This works also fine:

这也很好用:

df_merged['price_new'] = 0

for i, row in df_merged.iterrows():
     df_merged.loc[i,'price_new']  = i 

回答by Deepak

This is not an error. It simply saying that the data frame _merged is initialised as a view of a parent daraframe and thus isn't a data frame by itself, therefore cannot take values. That's probably why when you check the value of the merged data frame after this step it remains the same as the original. You have two options: make your _merged data frame itself a copy by using the .copy() method when you initialise it from its parent data frame. Or in the loop or the computation set the values to the parent data frame using the same calculations or indexes done on merged data frame. I'd recommend the first method because I don't think memory is a constraint for you and you want the values changed in the new data frame. Plus it is as straightforward as can be.

这不是错误。它只是说数据帧 _merged 被初始化为父 daraframe 的视图,因此本身不是数据帧,因此不能取值。这可能就是为什么在此步骤之后检查合并数据框的值时它与原始值保持相同的原因。您有两个选择:当您从其父数据帧初始化它时,使用 .copy() 方法使您的 _merged 数据帧本身成为一个副本。或者在循环或计算中,使用在合并数据框上完成的相同计算或索引将值设置为父数据框。我推荐第一种方法,因为我不认为内存对您来说是一个限制,并且您希望在新数据框中更改值。此外,它尽可能简单。

回答by tartaruga_casco_mole

For a loop update in pandas dataframe:

对于Pandas数据框中的循环更新:

for i, row in df_merged.iterrows():
     df_merged.set_value(i,'new_value',i)

Should be able to update values in pandas dataframe.

应该能够更新Pandas数据框中的值。

FutureWarning: set_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead.

FutureWarning: set_value 已弃用,将在未来版本中删除。请改用 .at[] 或 .iat[] 访问器。

for i, row in df_merged.iterrows():
     df_merged.at[i,'new_value'] = i

Should be preferred.

应该是首选。

回答by Mathew Savage

If you want to perform a multiplication on two columns, you don't have to do it row-wise, the following should work:

如果要对两列执行乘法,则不必按行进行,以下应该有效:

df_merged['new_value'] = df_merged['move_%'] * df_merged['value']