2017-01-02 12 views
1

私はJupyterで処理しようとしているデータフレームを持っています。このデータフレームは元々空白が見つかるNaNで埋められていましたが、私はを無視して問題が発生していたので、それらを 'ヌル'文字列に置き換えることにしました。Pythonで配列の条件に一致するすべての要素を取得する

次のコードは、私がどこかにフィルタリングするために供給することができるアイデアは、すべての要素がNaN(以降、「NULL」)ではない配列を構築することで、元のファイルのサンプル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" 

ありますelse。

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']] 

である私はすべてのフィードバックに感謝

  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' 'BLI' '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

関連する問題