2016-10-23 3 views
1

주로 팬더를 사용하여 Python 2.7을 사용하여 사람 분류 데이터를 정리합니다. 'background_color'과 같은 필드에 일부 사람들이 수레를 입력했기 때문에 numpy.isreal()을 사용하여 수표를 확인합니다. 어쨌든, 현재 설정되어있는 하나의 색상에 대해 어떻게 보이는지 예제를 게시하고 있습니다. 작동하지 않습니다. 루프가 끝날 때까지 파이썬이 보이지 않습니다. blues은 모든 인덱스의 목록입니다. background_color는 대소 문자를 구분하지 'BLUE' 어디에 :이 더 파이썬과 예쁘게을지도 기능을 사용할 수 있습니다처럼더 많은 데이터 청소 루프 만들기 Python

blueShapes=[] 
for i in range(imageData.shape[0]): 
    if not (np.isreal(imageData.loc[i,'background_color'])): 
     if imageData.loc[i,'background_color'].upper()=='BLUE': 
      blueShapes.append(i) 

보인다. 내가 말했듯이, 그것은 의도 한대로 작동하지만 그냥 너무 ... C 또는 Java 파이썬으로 작성된 것 같습니다. 모든 응답에 미리 감사드립니다.

- 편집 : 그것은 오래된 루프에서 유물이

+0

'수 = LEN (blueShapes)'당신을 저장해야합니다 일부'chars' – Wasi

답변

0

당신이 위의 경우 개수

len(subset['background_color']) 
+0

당신 내가 이겼다 있는지, 내가 문자열 객체에'.astype (STR)를'호출 할 때 t 오류로 실행? – ThisGuyCantEven

+0

맞지 않는데, 'lambda'없이 사용하려고했을 때 예외가 발생했습니다. – ThisGuyCantEven

+0

소원 나는 upvote에 충분한 담당자가있어, 고마워! – ThisGuyCantEven

0

에 대한

imageData['background_color_2'] = map(lambda x: x.upper(), imageData['background_color'].astype(str)) 

subset = imageData[imageData['background_color_2']=='BLUE'] 

와 BEW 열을 만들 수 있었기 때문에 나는 카운트를 제거 특정 문자열 값을 갖는 행의 인덱스를 반환하는 람다 함수를 정의 할 수 있습니다.

getRowIndexWithStringColor = lambda df, color: [i for i in range(df.shape[0]) if (not np.isreal(df.loc[i,'background_color'])) and df.loc[i,'background_color'].upper()==color)] 
rowIndexWithBlue = getRowIndexWithStringColor(imageData, 'BLUE') 
0

일반적으로 팬더에서 반복하는 경우 잘못하고 있습니다. (그래서 당신이 그것을 적용 할 필요가 안된 불구하고!)

은 다음과 비슷한 모습이 될 것

strings = (~imageData.background_color.apply(np.isreal)) 
blue = (imageData.background_color.str.upper()=="BLUE") 
blueshapes = imageData[strings & blue].index   
0

감사합니다 모두! 나는 Steven G의 대답에 약간의 적응을 사용했다. 나는이 모든 것을 마스터 .csv에 백업했다. 따라서 background_color 컬럼을 겹쳐 쓰는 것만으로도 문제가 없었다. 어쨌든 비 - sring 항목은 무효하지만 그들은 혼자가 아니므로 나중에 모든 색인의 색인을 연결 한 후에 남은 색인으로 발견 할 것입니다.

imageData['background_color']=map(lambda x: x.upper(), imageData['background_color'].astype(str)) 

blueShapes=imageData[imageData['background_color']=='BLUE'].index 
0

나는 기능으로 전원을 켜고 배열을 반환 다음과 같이 각각의 목록이 추출됩니다.

구글 : 파이썬

젠은 당신에게 빠른 참조 python list/dict/set over map/filter주기.

가독성과 클리너 코드.

def colorShapes(color): 
    return [i 
      for i in range(imageData.shape[0]) 
      if not(np.isreal(imageData.loc[i, 'background_color'].upper() == color 
      and imageData.loc[i, 'background_color'].upper() == color]