2017-12-19 3 views
-1

私は値のpythonパンダでどのようにpandas、pythonで特定のオブジェクトを持つ行を削除しますか?

はい

はい

どのように私は希望のこれらの種類が含まれている私のデータの列を持っています文字列を含む行全体を識別し、行全体を削除または削除しますか?

おかげ

+0

df.drop(df ['col'] == 2).index) –

+0

完了した行のみを返すようですか? – KRidg

+0

値が見つからなかったすべての行が移入されました。感謝!! – KRidg

答えて

1

IIUC:

df = df[~pd.to_numeric(df['col'], errors='coerce').isna()] 

または

df = df[pd.to_numeric(df['col'], errors='coerce').notna()] 
+0

私は両方を試しましたが、このエラーが発生しました 'DataFrame'オブジェクトには 'to_numeric'属性がありません – KRidg

+0

@KRidg、あなたのDataFrameに 'pd 'という名前を付けましたか? 'pd'はpandasモジュールのエイリアスとされています... – MaxU

+0

いいえ、私はそうは思わない、私のDataFrameはdfの代わりに置く全く別の名前を持っています。私は同じ位置にpdを残しました。 – KRidg

0

はここでこれを解決する方法です。 DataFrameを構築するためのコードを含めました。 文字列の値を見つけてドロップします。 df.dropは、inplace = Trueを指定しない限り、元のdfの変更されたコピー を返します。必要に応じて、df df = df.drop...の元の名前を割り当てるか、inplace引数を使用します。

import pandas as pd 
import numpy as np 
# build the df 
df = pd.DataFrame({'col_name' : [2, 2, 'yes', 2, 'yes']}) 
print(df) 

    col_name 
0  2 
1  2 
2  yes 
3  2 
4  yes 

# ask what are the types in column 
df.col_name.apply(type) 

0 <class 'int'> 
1 <class 'int'> 
2 <class 'str'> 
3 <class 'int'> 
4 <class 'str'> 
Name: col_name, dtype: object 

# you can see that the strings are in row 2 and 4 
# and drop them like this 
df.drop([2, 4], axis='rows') 

    col_name 
0  2 
1  2 
3  2 
関連する問題