私は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]
できます。
あなたの提案は '[['BLA' 'BLE' 'BLI' 'BLO' 'BLU' 'Null' 'Null' 'Null' ']]'を生成します。次元数が多すぎると、私は間違って何をしていますか? – Sosi
@ Jean-FrançoisFabreご協力いただきありがとうございます。私はそのプリントの出力を元の投稿に追加しました – Sosi
@ Jean-FrançoisFabre心配しなくても、あなたの助けに感謝します。元の投稿にサンプルを追加しました。もう一度ありがとうございます – Sosi