Python 使用索引为 Pandas DataFrame 中的特定单元格设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13842088/
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
Set value for particular cell in pandas DataFrame using index
提问by Mitkp
I've created a Pandas DataFrame
我创建了一个 Pandas DataFrame
df = DataFrame(index=['A','B','C'], columns=['x','y'])
and got this
得到了这个
x y A NaN NaN B NaN NaN C NaN NaN
Then I want to assign value to particular cell, for example for row 'C' and column 'x'.
I've expected to get such result:
然后我想为特定的单元格分配值,例如为“C”行和“x”列。我期望得到这样的结果:
x y A NaN NaN B NaN NaN C 10 NaN
with this code:
使用此代码:
df.xs('C')['x'] = 10
but contents of dfhaven't changed. It's again only NaNs in DataFrame.
但内容df没有改变。它再次仅NaN在 DataFrame 中。
Any suggestions?
有什么建议?
采纳答案by unutbu
RukTech's answer, df.set_value('C', 'x', 10), is far and away faster than the options I've suggested below. However, it has been slated for deprecation.
RukTech 的回答,df.set_value('C', 'x', 10)比我在下面建议的选项要快得多。但是,它已被弃用。
Going forward, the recommended method is .iat/.at.
展望未来,推荐的方法是.iat/.at。
Why df.xs('C')['x']=10does not work:
为什么df.xs('C')['x']=10不起作用:
df.xs('C')by default, returns a new dataframe with a copyof the data, so
df.xs('C')默认情况下,返回一个带有数据副本的新数据帧,因此
df.xs('C')['x']=10
modifies this new dataframe only.
仅修改此新数据框。
df['x']returns a view of the dfdataframe, so
df['x']返回df数据框的视图,所以
df['x']['C'] = 10
modifies dfitself.
修改df自己。
Warning: It is sometimes difficult to predict if an operation returns a copy or a view. For this reason the docs recommend avoiding assignments with "chained indexing".
警告:有时很难预测操作是返回副本还是视图。出于这个原因,文档建议避免使用 "chained indexing" 进行分配。
So the recommended alternative is
所以推荐的替代方案是
df.at['C', 'x'] = 10
which doesmodify df.
这确实修改了df.
In [18]: %timeit df.set_value('C', 'x', 10)
100000 loops, best of 3: 2.9 μs per loop
In [20]: %timeit df['x']['C'] = 10
100000 loops, best of 3: 6.31 μs per loop
In [81]: %timeit df.at['C', 'x'] = 10
100000 loops, best of 3: 9.2 μs per loop
回答by Yariv
The recommended way (according to the maintainers) to set a value is:
设置值的推荐方法(根据维护者)是:
df.ix['x','C']=10
Using 'chained indexing' (df['x']['C']) may lead to problems.
使用“链式索引”( df['x']['C']) 可能会导致问题。
See:
看:
回答by RukTech
Update: The .set_valuemethod is going to be deprecated. .iat/.atare good replacements, unfortunately pandas provides little documentation
更新:该.set_value方法将被弃用。.iat/.at是很好的替代品,不幸的是熊猫提供的文档很少
The fastest way to do this is using set_value. This method is ~100 times faster than .ixmethod. For example:
最快的方法是使用set_value。这种方法比.ix方法快 100 倍。例如:
df.set_value('C', 'x', 10)
df.set_value('C', 'x', 10)
回答by Yash
Try using df.loc[row_index,col_indexer] = value
尝试使用 df.loc[row_index,col_indexer] = value
回答by Alon Galor
回答by Joshua Maga?a
I too was searching for this topic and I put together a way to iterate through a DataFrame and update it with lookup values from a second DataFrame. Here is my code.
我也在寻找这个主题,我整理了一种方法来遍历 DataFrame 并使用来自第二个 DataFrame 的查找值更新它。这是我的代码。
src_df = pd.read_sql_query(src_sql,src_connection)
for index1, row1 in src_df.iterrows():
for index, row in vertical_df.iterrows():
src_df.set_value(index=index1,col=u'etl_load_key',value=etl_load_key)
if (row1[u'src_id'] == row['SRC_ID']) is True:
src_df.set_value(index=index1,col=u'vertical',value=row['VERTICAL'])
回答by Blairg23
You can also use a conditional lookup using .locas seen here:
您还可以使用条件查找,.loc如下所示:
df.loc[df[<some_column_name>] == <condition>, [<another_column_name>]] = <value_to_add>
where <some_column_nameis the column you want to check the <condition>variable against and <another_column_name>is the column you want to add to (can be a new column or one that already exists). <value_to_add>is the value you want to add to that column/row.
哪里<some_column_name是您要检查<condition>变量<another_column_name>的列,是您要添加到的列(可以是新列或已存在的列)。<value_to_add>是要添加到该列/行的值。
This example doesn't work precisely with the question at hand, but it might be useful for someone wants to add a specific value based on a condition.
这个例子不能准确地处理手头的问题,但对于想要根据条件添加特定值的人来说可能很有用。
回答by Muge Cevik
you can use .iloc.
你可以使用.iloc.
df.iloc[[2], [0]] = 10
回答by Kirill Dolmatov
If you want to change values not for whole row, but only for some columns:
如果您不想更改整行的值,而只想更改某些列的值:
x = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
x.iloc[1] = dict(A=10, B=-10)
回答by andrei deusteanu
From version 0.21.1 you can also use .atmethod. There are some differences compared to .locas mentioned here - pandas .at versus .loc, but it's faster on single value replacement
从 0.21.1 版本开始,您还可以使用.at方法。与.loc此处提到的相比有一些差异-熊猫 .at 与 .loc,但单值替换更快

