2016-10-28 13 views
1

私はこのような混合値を持つ系列Sを持っている場合は要素がJSON形式であるとき、それはスキップするようにパンダシリーズの要素で条件付き分岐を実行するにはどうすればよいですか?

textelement 
{"id":1,"name":"whatever","value":"sonso"} 
name-value 

は、どのように私は条件文を作るのですが、それはテキスト文字列または名 - だとき値のペア、JSON形式に変換しますか?

+0

Whasは、サンプルデータからフィルタリングされた出力からJSONを望まれていますか? – jezrael

答えて

1

あなたはstr.startswithによってboolean indexingmaskによってjsonフォーマットを除外することができる:

s = pd.Series(['textelement',{"id":1,"name":"whatever","value":"sonso"}, 'name-value']) 
print(s) 
0          textelement 
1 {'id': 1, 'value': 'sonso', 'name': 'whatever'} 
2           name-value 
dtype: object 

#cast all values to string 
s = s.astype(str) 

#check which string starts with `{` 
mask = s.str.startswith('{') 

print (mask) 
0 False 
1  True 
2 False 
dtype: bool 

print (~mask) 
0  True 
1 False 
2  True 
dtype: bool 

#filter by inverted mask with ~  
s = s[~mask] 

print (s) 
0 textelement 
2  name-value 
dtype: object 
+0

素晴らしいですが、テキスト文字列と名前と値のペアを変換した後にJSONのすべての要素を出力したいのですが? – socnis

+0

それから 's1 = s [マスク]' – jezrael

+0

と 's2 = s [〜マスク]' – jezrael

関連する問題