[英]Iteratively change every cell in a column of a Pandas dataframe
I am trying to change the value of every cell in a Pandas data frame. 我正在尝试更改Pandas数据框中每个单元格的值。 Expecting
.loc
to allow me to identify a cell with the paradigm df.loc[row_index, column_name] = cell value
, I've used the following loop: 期望
.loc
允许我使用范例df.loc[row_index, column_name] = cell value
来标识一个单元df.loc[row_index, column_name] = cell value
,我使用了以下循环:
table["field"] = 6 #placehodler value used only to create the column
for field, index in enumerate(table["field"]): table.loc[index,
"field"] = table.loc[index, "field_x"] if math.isnan(table.loc[index,
"field_y"]) else table.loc[index, "field_y"]
However, I'm getting the following error: KeyError: 'the label [6] is not in the [index]'
. 但是,出现以下错误:
KeyError: 'the label [6] is not in the [index]'
。 What is the correct syntax for selecting values by index? 通过索引选择值的正确语法是什么?
You shouldn't be using enumerate
to generate the index and column values, you should use iterrows
. 您不应使用
enumerate
来生成索引和列值,而应使用iterrows
。
Example usage: 用法示例:
In [6]:
df = pd.DataFrame({'a':np.arange(5), 'b':np.random.randn(5)})
df
Out[6]:
a b
0 0 -0.579585
1 1 -0.582196
2 2 -0.367147
3 3 -0.363332
4 4 0.880826
In [9]:
for index, row in df.iterrows():
print('index: ', index)
print('row: ', row)
output: 输出:
index: 0
row: a 0.000000
b -0.579585
Name: 0, dtype: float64
index: 1
row: a 1.000000
b -0.582196
Name: 1, dtype: float64
index: 2
row: a 2.000000
b -0.367147
Name: 2, dtype: float64
index: 3
row: a 3.000000
b -0.363332
Name: 3, dtype: float64
index: 4
row: a 4.000000
b 0.880826
Name: 4, dtype: float64
This will allow you to access a specifc row using df.loc[index]
, if you need the columns you can access using row.index
in the above for
loop 这将允许您使用
df.loc[index]
访问特定的行,如果需要这些列,则可以使用上述for
循环中的row.index
进行访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.