简体   繁体   English

为Pandas DataFrame列的一行分配值

[英]Assigning a value to a single row for a Pandas DataFrame column

I am trying to reassign a value in a single row of a column in a Pandas DataFrame. 我正在尝试在Pandas DataFrame的列的单行中重新分配值。

import pandas as pd
import numpy as np

Here's the DataFrame: 这是DataFrame:

test_df = pd.DataFrame({'range_total' : [3000,3000,3000,3000,3000,3000,0,2000,2000,1000,1000,1000,1000,1000,1000],
    'high_boundary' : [6,6,6,6,6,6,7,9,9,15,15,15,15,15,15],
    'dist_num' : [1197, 142, 142, 1197, 159, 159, 0, 1000, 1000, 398, 50, 50, 398, 50, 50],
    'round_num_sum' : [2996, 2996, 2996, 2996, 2996, 2996, 0, 2000, 2000, 996, 996, 996, 996, 996, 996]})

In my code I subset the DataFrame for each value of high_boundary and find the index(es) of the test_df corresponding to the greatest value of dist_num (picking the first if tied). 在我的代码中,我为high_boundary的每个值high_boundary了DataFrame的子集,并找到了与test_df相对应的test_df的索引(如果绑定, dist_num选择第一个)。 For this example, I set the index to: 对于此示例,我将索引设置为:

sub_idx = 0 

I can access the value using this (and other similar versions) code: 我可以使用以下(和其他类似版本)代码访问值:

test_df.ix[(test_df.high_boundary == 6), "dist_num"][sub_idx]

that returns: 返回:

1197

But assigning a new value fails: 但是分配新值失败:

test_df.ix[(test_df.high_boundary == 6), "dist_num"][sub_idx] = 42
test_df.ix[(test_df.high_boundary == 6), "dist_num"][sub_idx]

which still returns: 仍然返回:

1197 

But: 但:

test_df.ix[(test_df.high_boundary == 6), "dist_num"] = 42
test_df.ix[(test_df.high_boundary == 6), "dist_num"]

returns: 收益:

0    42
1    42 
2    42
3    42
4    42
5    42
Name: dist_num, dtype: int64

I appreciate any help. 感谢您的帮助。 This is my first post, because I've always found what I've needed on SO until now. 这是我的第一篇文章,因为到目前为止,我一直都在SO上找到所需的东西。 I'm using version 0.14.0. 我正在使用版本0.14.0。

Sometimes you can get copy of (part of) original dataframe test_df . 有时,您可以获得原始数据帧test_df (的一部分)的test_df

Especially if you select elements using [...][...] 特别是如果您使用[...][...]选择元素

So you changed one value in copy, not in oryginal test_df 因此,您更改了副本中的一个值,而不是原始的test_df

Try for example this: 例如尝试:

test_df["dist_num"].ix(test_df.high_boundary == 6)[sub_idx] = 0

and you should get expected result. 并且您应该得到预期的结果。

Had similar problems in past. 过去也有类似的问题。 Suggest you work though examples at 建议您通过以下示例工作

http://pandas.pydata.org/pandas-docs/stable/indexing.html http://pandas.pydata.org/pandas-docs/stable/indexing.html

specifically the section http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy will help you. 特别是http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy这一节将为您提供帮助。

The editorial explanation is that if you are chain slicing using df[][] constructs you frequently are slicing to series 1st and then to values. 社论上的解释是,如果要使用df [] []构造进行链式切片,那么您经常会切片到系列1st,然后切片到值。 it is not feasible for pandas to keep track of your original filters to allow you to write back to the slice. 大熊猫跟踪原始过滤器以允许您写回切片是不可行的。

short answer try using a single operator like ".loc" to do selections that you intend to assign to. 简短答案请尝试使用单个运算符(例如“ .loc”)来进行要分配给您的选择。

When revisiting this code after a few years, I found that the solution posted above now provides an error (using Pandas version 0.20.1 and Python 2.7.13): TypeError: 'Series' objects are mutable, thus they cannot be hashed . 几年后重新访问此代码时,我发现上面发布的解决方案现在提供了一个错误(使用Pandas版本0.20.1和Python 2.7.13): TypeError: 'Series' objects are mutable, thus they cannot be hashed In case anyone else has this problem, I've added a solution below. 万一其他人有这个问题,我在下面添加了一个解决方案。

To update a single element of a subset of a pd.DataFrame , the index values from the subset were found and then the index corresponding to desired row was used to select the element to update 要更新pd.DataFrame子集的单个元素,请找到该子集的索引值,然后使用与所需行相对应的索引来选择要更新的元素

sub_idx = 0

indices = test_df.loc[test_df.high_boundary == 6,"dist_num"].index
print(test_df.loc[indices[sub_idx],"dist_num"])
# 1197
test_df.loc[indices[sub_idx],"dist_num"] = 0 

print(test_df.loc[indices[sub_idx],"dist_num"])
# 0

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

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