2017-09-01 97 views
0

我有一個數組表示我想標記的行數。使用熊貓在數據框上寫入特定行

example_array = [3, 101, 505, 1020, 3500] 

裏面一個數據幀,我有一個稱爲DF [「指標」]列名,並會作爲example_array注意到一個「問題」的字符串,以紀念該列的特定行。基本上,在df [「indicator」]的第103行,第101行,等等,我想把它標記爲一個問題。

有沒有一種方法可以做到這一點?

回答

1

使用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   
+1

這是我會怎麼做。或者,如果索引不是數字,你可以使用'iloc':'df.iloc [example_array,df.columns ==「indicator」] =「問題」 – johnchase