2017-11-28 24 views
0

対応する列の内容が条件を満たすインデックスの範囲を取得する最も効率的な方法は、タグで始まりbodyタグで終わる行のようなものです。例えば、データフレームのためのパンダのデータフレームのインデックスの範囲を取得する方法

私は行のインデックスに1-3

を取得したいこの

のように見える誰もがこれを達成するための最も神託の方法を提案することはできますか?

import pandas as pd 

df=pd.DataFrame([['This is also a interesting topic',2],['<body> the valley of flowers ...',1],['found in the hilly terrain',5], 
      ['we must preserve it </body>',6]],columns=['description','count']) 

print(df.head()) 
+0

コードまたはデータの画像を投稿しないでください。 ["誰かが私を助けることができますか?"実際の質問ではありませんか?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)。 – wwii

+0

@wwii私はそれを念頭に置いています。情報をありがとう。 – user765160

答えて

1

どのような条件を満たしたいと思いますか?ここ

import pandas as pd 

df=pd.DataFrame([['This is also a interesting topic',2],['<body> the valley of flowers ...',1],['found in the hilly terrain',5], 
      ['we must preserve it </body>',6]],columns=['description','count']) 
print(df) 
print(len(df[df['count'] != 2].index)) 

df['count'] != 2サブセットDF、及びlen(df.index)インデックスの長さを返します。

更新;明示的に開始文字列や終了文字列を探すのではなく、str.contains()を使用しました。

df2 = df[(df.description.str.contains('<body>') | (df.description.str.contains('</body>')))] 
print(df2) 
print(len(df2.index)) 

から助け:Check if string is in a pandas dataframe

+0

申し訳ありません、質問に条件を追加するのを忘れました。私が探している条件は、開始行と終了行、タグ – user765160

+0

はい、それは私が望むものです。 – user765160

関連する問題