2016-07-28 55 views
2

我對熊貓有問題,看到處處都是,但覺得我忽略了一些東西。用熊貓編輯特定的單元格[Python]

我有一個csv文件,我導入到熊貓,它有一個ID列和另一列,我會打電話給列2.我想: 1.輸入一個ID到python。 2.搜索這個ID與熊貓ID列,並投入了1對相鄰的單元格,在第2列

import pandas 

csvfile = pandas.read_csv('document1.csv') 

#Convert everything to string for simplicity 
csvfile['ID'] = csvfile['ID'].astype(str) 

#Fill in all missing NaN 
csvfile = csvfile.fillna('missing') 

#looking for the row in which the ID '10099870.0' is in. 
indexid = csvfile.loc[csvfile['ID'] == '10099870.0'].index 

# Important part! I think this selects the column 2, row 'indexid' and replaces missing with 1. 
csvfile['Column 2'][indexid].replace('missing', '1') 

我知道這是一個簡單的問題,但感謝您的幫助!

回答

1

這是我會怎麼做:

cond = csvfile.ID == '10099870.0' 
col = 'Column 2' 
csvfile.loc[cond, col] = csvfile.loc[cond, col].replace('missing', '1') 
+0

太謝謝你了!這工作完美 – jmramosfran

+0

我做到了,謝謝!不過,我收到了這條消息(我必須繼續發帖/幫助!):感謝您的反饋!記錄下名聲低於15的人的投票記錄,但不要更改公開顯示的帖子分數。 – jmramosfran

+0

完美!再次感謝piRSquared – jmramosfran