使用熊猫在数据框上写入特定行
问题描述:
我有一个数组表示我想标记的行数。使用熊猫在数据框上写入特定行
example_array = [3, 101, 505, 1020, 3500]
里面一个数据帧,我有一个称为DF [“指标”]列名,并会作为example_array注意到一个“问题”的字符串,以纪念该列的特定行。基本上,在df [“indicator”]的第103行,第101行,等等,我想把它标记为一个问题。
有没有一种方法可以做到这一点?
答
使用loc
赋值;传递example_array
作为行索引和indicator
为列索引:
>>> df = pd.DataFrame({"indicator": [""]*5})
>>> df
indicator
0
1
2
3
4
>>> example_array = [0,3]
>>> df.loc[example_array, "indicator"] = "problem"
>>> df
indicator
0 problem
1
2
3 problem
4
这是我会怎么做。或者,如果索引不是数字,你可以使用'iloc':'df.iloc [example_array,df.columns ==“indicator”] =“问题” – johnchase