Python 使用 iloc 为 pandas DataFrame 中的特定单元格设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31569384/
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-08-19 10:14:06  来源:igfitidea点击:

Set value for particular cell in pandas DataFrame with iloc

pythonpandas

提问by luna1999

I have a question similar to thisand this. The difference is that I have to select row by position, as I do not know the index.

我有一个类似于thisthis 的问题。不同之处在于我必须按位置选择行,因为我不知道索引。

I want to do something like df.iloc[0, 'COL_NAME'] = x, but iloc does not allow this kind of access. If I do df.iloc[0]['COL_NAME] = xthe warning about chained indexing appears.

我想做类似的事情df.iloc[0, 'COL_NAME'] = x,但 iloc 不允许这种访问。如果我这样做df.iloc[0]['COL_NAME] = x,就会出现关于链式索引的警告。

采纳答案by Jianxun Li

For mixed position and index, use .ix. BUT you need to make sure that your index is not of integer, otherwise it will cause confusions.

对于混合位置和索引,请使用.ix. 但是你需要确保你的索引不是整数,否则会引起混淆。

df.ix[0, 'COL_NAME'] = x

Update:

更新:

Alternatively, try

或者,尝试

df.iloc[0, df.columns.get_loc('COL_NAME')] = x

Example:

例子:

import pandas as pd
import numpy as np

# your data
# ========================
np.random.seed(0)
df = pd.DataFrame(np.random.randn(10, 2), columns=['col1', 'col2'], index=np.random.randint(1,100,10)).sort_index()

print(df)


      col1    col2
10  1.7641  0.4002
24  0.1440  1.4543
29  0.3131 -0.8541
32  0.9501 -0.1514
33  1.8676 -0.9773
36  0.7610  0.1217
56  1.4941 -0.2052
58  0.9787  2.2409
75 -0.1032  0.4106
76  0.4439  0.3337

# .iloc with get_loc
# ===================================
df.iloc[0, df.columns.get_loc('col2')] = 100

df

      col1      col2
10  1.7641  100.0000
24  0.1440    1.4543
29  0.3131   -0.8541
32  0.9501   -0.1514
33  1.8676   -0.9773
36  0.7610    0.1217
56  1.4941   -0.2052
58  0.9787    2.2409
75 -0.1032    0.4106
76  0.4439    0.3337

回答by AZhao

If you know the position, why not just get the index from that?

如果您知道位置,为什么不直接从中获取索引?

Then use .loc:

然后使用.loc

df.loc[index, 'COL_NAME'] = x

回答by ford prefect

One thing I would add here is that the atfunction on a dataframe is much faster particularly if you are doing a lot of assignments of individual (not slice) values.

我要在这里添加的一件事是at数据帧上的函数要快得多,特别是如果您正在对单个(不是切片)值进行大量分配。

df.at[index, 'col_name'] = x

In my experience I have gotten a 20x speedup. Hereis a write up that is Spanish but still gives an impression of what's going on.

根据我的经验,我获得了 20 倍的加速。是一篇西班牙文的文章,但仍然给人一种对正在发生的事情的印象。

回答by Karl I.

Another way is to get the row index and then use df.loc or df.at.

另一种方法是获取行索引,然后使用 df.loc 或 df.at。

# get row index 'label' from row number 'irow'
label = df.index.values[irow] 
df.at[label, 'COL_NAME'] = x

回答by ricmarchao

another way is:

另一种方法是:

df["COL_NAME"].iloc[0]=x

回答by Om Prakash

Extending Jianxun's answer, using set_valuemehtod in pandas. It sets value for a column at given index.

扩展建勋的答案,set_value在熊猫中使用方法。它为给定索引处的列设置值。

From pandas documentations:

来自熊猫文档:

DataFrame.set_value(index, col, value)

DataFrame.set_value(index, col, value)

To set value at particular index for a column, do:

要在列的特定索引处设置值,请执行以下操作:

df.set_value(index, 'COL_NAME', x)

Hope it helps.

希望能帮助到你。

回答by DINA TAKLIT

You can use:

您可以使用:

df.set_value('Row_index', 'Column_name', value)

set_valyeis ~100 times faster than .ixmethod. It also better then use df['Row_index']['Column_name'] = value.

set_valye.ix方法快 100 倍。它也更好地使用 df['Row_index']['Column_name'] = value.

But since set_valueis deprecatednow so .iat/.atare good replacements.

但由于set_value现在已弃用,所以.iat/.at是很好的替代品。

For example if we have this data_frame

例如,如果我们有这个 data_frame

   A   B   C
0  1   8   4 
1  3   9   6
2  22 33  52

if we want to modify the value of the cell [0,"A"] we can do

如果我们想修改单元格 [0,"A"] 的值,我们可以这样做

df.iat[0,0] = 2

or df.at[0,'A'] = 2

或者 df.at[0,'A'] = 2

回答by Phil

To modify the value in a cell at the intersection of row "r" (in column "A") and column "C"

修改“r”行(“A”列)和“C”列交叉处的单元格中的值

  1. retrieve the index of the row "r" in column "A"

        i = df[ df['A']=='r' ].index.values[0]
    
  2. modify the value in the desired column "C"

        df.loc[i,"C"]="newValue"
    
  1. 检索“A”列中“r”行的索引

        i = df[ df['A']=='r' ].index.values[0]
    
  2. 修改所需列“C”中的值

        df.loc[i,"C"]="newValue"
    

Note: before, be sure to reset the index of rows ...to have a nice index list!

注意:之前,一定要重置行的索引......以获得一个漂亮的索引列表!

        df=df.reset_index(drop=True)