python - iterating on glob set doesn't work with if condition -


l have column composed of set of string follow :

npa = pd.read_csv("file_names.csv", usecols=[3,5,6, 7, 8, 9], header=none) npa.iloc[:,0] xml_0_1841729699_001 xml_0_1841729699_00nn xml_0_1841729699_00145 xml_0_1841729699_00145 xml_0_1841729699_00178 xml_0_1841729699_001jklm xml_0_1841729699_001fjmfd 

and l have png names follow :

path_img = "/images" os.chdir(path_img) images_name = glob.glob("*.png") set_img = set([x.rsplit('.', 1)[0] x in images_name]) set_img set(['xml_0_1841729699_001fjmfd', xml_0_1841729699_00145','xml_0_1841729699_001','xml_0_1841729699_00178']) 

l want check name in set_img matches 1 in dataframe before doing processing :

for in range(1, 30):     img_name in set_img:         if (img_name==npa.iloc[i,0]):  # 0 corresponds the column of string              print("it works") 

however doesn't check condition if. what's wrong ?

edit1:

f = open("file_names.csv", 'rt') reader = csv.reader(f) row in reader:     if cpt >= 1:  # skip header         characs.append(str(row[5]))     cpt += 1  path_img = "/images" os.chdir(path_img) images_name = glob.glob("*.png") set_img = set([x.rsplit('.', 1)[0] x in images_name]) mask = npa.iloc[:,0].isin(set_img) img in set_img:      img = cv2.imread(path_img+'/'+ img +'.png')     print(img.shape)      images = []     images_names = []     width=[]     height=[]      in range(1, nb_charac):         if (img==npa[mask].iloc[i,0]):             print("hello")             coords = npa.iloc[[i]]             charac = characs[i - 1] 

l got following errors :

 futurewarning: elementwise comparison failed; returning scalar instead, in future perform elementwise comparison   if (img==npa[mask].iloc[i,0]): traceback (most recent call last):   file "/to_test.py", line 186, in <module>     if (img==npa[mask].iloc[i,0]):   file "/usr/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1225, in __getitem__     return self._getitem_tuple(key)   file "/usr/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1449, in _getitem_tuple     self._has_valid_tuple(tup)   file "/usr/lib/python2.7/dist-packages/pandas/core/indexing.py", line 127, in _has_valid_tuple     if not self._has_valid_type(k, i):   file "/usr/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1417, in _has_valid_type     return self._is_valid_integer(key, axis)   file "/usr/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1431, in _is_valid_integer     raise indexerror("single positional indexer out-of-bounds") indexerror: single positional indexer out-of-bounds  edit2: 

then l replaced :

if (img==npa[mask].iloc[i,0]): 

by

if (img==npa[mask][3][i]): 

it works until row , l got following error :

    if (img==npa[mask][3][i]):   file "/usr/lib/python2.7/dist-packages/pandas/core/series.py", line 557, in __getitem__     result = self.index.get_value(self, key)   file "/usr/lib/python2.7/dist-packages/pandas/core/index.py", line 1790, in get_value     return self._engine.get_value(s, k)   file "pandas/index.pyx", line 103, in pandas.index.indexengine.get_value (pandas/index.c:3204)   file "pandas/index.pyx", line 111, in pandas.index.indexengine.get_value (pandas/index.c:2903)   file "pandas/index.pyx", line 157, in pandas.index.indexengine.get_loc (pandas/index.c:3843)   file "pandas/hashtable.pyx", line 303, in pandas.hashtable.int64hashtable.get_item (pandas/hashtable.c:6525)   file "pandas/hashtable.pyx", line 309, in pandas.hashtable.int64hashtable.get_item (pandas/hashtable.c:6463) keyerror: 2035 

use isin create boolean mask. use mask filter dataframe. equivalent looping through each row , checking if first column in set.

mask = npa.iloc[:,0].isin(set_img) npa[mask] 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -