Home > Net >  column value parse json
column value parse json

Time:10-06

ive got data drame

json = {'contexts_ru_andata_master_cookies_1': {0: [{'_ym_uid': '1664978572350562652'}],
  1: [{'_ym_uid': '1664978577951178500'}],
  2: [{'_ym_uid': '1631015476823239589'}],
  3: [{'_ym_uid': '1664945479855475653'}],
  4: [{'_ym_uid': '1663327749550707020'}],
  6: [{'_ym_uid': '1664978547593809275'}],
  7: [{'_ym_uid': '16649783691007078342'}],
  8: [{'_ym_uid': '1662551949642530804'}]}}


pd.DataFrame.from_dict(json)

i need to get numeric value from cell, any help will be appreciated. like 1664978577951178500 and etc

CodePudding user response:

It's not a corret json string, you can use "re" to match it.

import re

json = '''{'contexts_ru_andata_master_cookies_1': {0: [{'_ym_uid': '1664978572350562652'}],
  1: [{'_ym_uid': '1664978577951178500'}],
  2: [{'_ym_uid': '1631015476823239589'}],
  3: [{'_ym_uid': '1664945479855475653'}],
  4: [{'_ym_uid': '1663327749550707020'}],
  6: [{'_ym_uid': '1664978547593809275'}],
  7: [{'_ym_uid': '16649783691007078342'}],
  8: [{'_ym_uid': '1662551949642530804'}]}}'''

res = re.finditer(r'\'[0-9]*\'', json)
cookies_l = []
for i in res:
    cookies_l.append(i.group()[1:-1])

print(cookies_l)

This is output:

['1664978572350562652', '1664978577951178500', '1631015476823239589', '1664945479855475653', '1663327749550707020', '1664978547593809275', '16649783691007078342', '1662551949642530804']

CodePudding user response:

df['_ym_uid'] = df['contexts_ru_andata_master_cookies_1'].str[0].str[0].apply(lambda x : x['_ym_uid'])

that was the answer

  • Related