2017-04-05 19 views
0

データフレームの列からパターンを抽出しています。一部には「オスカー」という言葉があり、一部には「オスカー」という言葉があります。パンダのデータフレームの抽出方法以下は抽出行コードです。これによりエラーが発生します。Pandasデータフレームの抽出パターン

df['Oscar_Awards_Won'] = df['Awards'].str.extract('Won (\d+) (Oscar[s]?)', expand=True).fillna(0) 

サンプルデータを送信していただきありがとうございます。列賞のサンプルデータ。私はオスカーの勝利を引き出すことを試みています。

Awards 
Won 3 Oscars. Another 234 wins & 312 nominations. 
Won 7 Oscars. Another 215 wins & 169 nominations. 
Won 11 Oscars. Another 174 wins & 113 nominations. 
Won 4 Oscars. Another 122 wins & 213 nominations. 
Won 3 Oscars. Another 92 wins & 150 nominations. 
Won 1 Oscar. Another 91 wins & 95 nominations. 
+5

私はサンプルデータと予想される出力が必要です。 [*** MCVE ***](http://stackoverflow.com/help/mcve)と[*** HowToAsk ***](http://stackoverflow.com/help/how-to-ask)を読み、 – piRSquared

+0

あなたの質問は、サンプルデータを提供していないため、下落してしまわなかったことは幸運です。 –

答えて

0

これは必要なのでしょうか?

import pandas as pd 
df = pd.DataFrame({'a': [1,2,3,4], 'b': ['is Oscar','asd','Oscars','not an Oscars q']}) 

df['c'] = ['Won 3 Oscars. Another 234 wins & 312 nominations.', 
'Won 7 Oscars. Another 215 wins & 169 nominations.', 
'Won 11 Oscar. Another 174 wins & 113 nominations.', 
'Won 4 Oscars. Another 122 wins & 213 nominations.'] 

このライン:

df['c'].str.extract('Won (\d+) Oscar[s]?', expand=True).fillna(0) 

を与える:

0 
0 3 
1 7 
2 11 
3 4 
+0

サンプルデータでは機能しません。私は上記のサンプルデータを投稿しました – Harish

+0

私にとってはうまくいきます。どのようなエラーが発生しますか? –

+0

oscarまたはoscarsの前に番号を取得する必要があります。それはオスカーのためだけに来ます。オスカーの前の数字は来ない。 – Harish

0

とにかく手紙のを心配する必要はありませんので、これも動作します。

df['Oscar_Awards_Won']=df['Awards'].str.extract('Won (\d+) Oscar', expand=True).fillna(0) 
関連する問題