2017-01-02 4 views
1

Jupyter에서 처리하려고하는 데이터 프레임이 있습니다. 이 데이터 프레임은 원래 공백이있는 NaN으로 채워지지만, 이후로는 'Null'문자열로 바꾸기로 결정했습니다 (이후 NaN을 무시하는 문제가 있었기 때문에). 없는 모든 요소가 NaN (또는 그 이후의 '널') 내가 먹을 수 그 곳 필터링 할 경우Python에서 배열의 조건과 일치하는 모든 요소 검색

다음 코드는 원본 파일의 샘플입니다 mydata.txt

##IGNORE THIS LINE 
group2,"BLA","BLE","BLI","BLO","BLU","TAT","TET","TOT","TUT" 
group0,"BLA","BLE","BLI","BLO","BLU" 
group3,"BLA","BLE","BLI" 

아이디어는 배열을 구축하는 것입니다 그밖에.

import rpy2.ipython 
import rpy2.robjects as robjects 
import pandas as pd 
import numpy 
import re #python for regex 
%load_ext rpy2.ipython 
%R 

path='C:/MyPath/' 

allgroups=pd.read_csv(path+'mydata.txt',sep=",",skiprows=1,header=None,index_col=0) 
allgroups=allgroups.fillna("Null") 

def groupdat(groupname): 
    #Cleans group 
    precleaned=numpy.array(allgroups.loc[[groupname]]) 
#  matching = [s for s in precleaned if s != "Null" ] #I tried this 
    matching=filter(lambda elem: elem != "Null",precleaned) #I also tried this. 
    print(matching) 
    return 

groupdat('group0') 

수율 위의 오류가 댓글을 달았 모두 matching

: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

precleaned의 출력은 인쇄 allgroups.loc[[groupname]]

[['BLA' 'BLE' 'BLI' 'BLO' 'BLU' 'Null' 'Null' 'Null' 'Null']] 

이다 I 모든 피드백을 이해할

  1  2  3  4  5  6  7  8  9 
0                 
group0 BLA BLE BLI BLO BLU Null Null Null Null 

[1 rows x 9 columns] 

준다.

+0

귀하의 제안은''[[ 'BLA' 'BLE' 'BLO' 'BLO' 'BLU' 'Null' 'Null' 'Null']''을 산출합니다. 너비가 너무 많으면 내가 뭘 잘못하고 있니? – Sosi

+0

@ Jean-FrançoisFabre 도움을 주셔서 대단히 감사합니다. 그 프린트의 출력물을 원래 게시물에 추가했습니다. – Sosi

+0

@ Jean-FrançoisFabre 걱정하지 마세요, 정말 감사드립니다. 나는 원래 게시물에 샘플을 추가했습니다. 다시 한번 감사드립니다 – Sosi

답변

1

당신이 당신의 배열을 만들 때 너무 많이 하나의 차원이

numpy.array(allgroups.loc[["group0"]]) 

그래서 listcomp/배열입니다 유일한 요소에 filter 반복, 당신은

만들

을 얻고 따라서 메시지 그것은이 좋아 :

numpy.array(allgroups.loc[["group0"][0]]) 

다음 [s for s in precleaned if s != "Null" ] 수율 :

['BLA', 'BLE', 'BLI', 'BLO', 'BLU'] 

예상대로입니다.

+0

실제로이 방법으로 문제가 해결됩니다. 감사! – Sosi

관련 문제