2

Data frame is below

id,validity
1941079,{'NT': 2017-02-10, 'T': None}
326809,None
1935569,{'NT': None, 'T': None}
1932997,{'NT': '2017-02-13', 'T': None}

Need to extract the id which NT is not None Expected out is below

df['not_none]

1941079
1932997
  • follow this link please. https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values – Ajay Kumar Oad Dec 27 '19 at 05:48

1 Answers1

0

Use Series.str.get with Series.notna and DataFrame.loc for filter with boolean indexing:

If necessary convert strings repr to dictionaries use custom function with try-except:

import ast

def parse_to_dict(x):
    try:
        return ast.literal_eval(x)
    except:
        return None

df['validity'] = df['validity'].apply(parse_to_dict)

s = df.loc[df['validity'].str.get('NT').notna(), 'id']
print (s)
0    1941079
3    1932997
Name: id, dtype: int64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252