0

I have following sample json file

{"id": 103, "data": [{"point": [10, 20], "sp": 2}, {"point": [20, 20], "sp": 3}, {"point": [10, 20], "sp": 0}, {"point": [30, 20], "sp": 0}]}

I read the json with json.load function and then perform

df = pd.DataFrame(filejson['data'])

if I print this df the data is shown as below using head() function

     Point  sp
0   [10, 20]   2
1   [20,20]  3

I would like to filter all null,zero from field "sp" so expecting output as

   sp
0   2
1   3

I would like to load only column "sp" and ignore all zero. is that possible?

user269867
  • 3,266
  • 9
  • 45
  • 65

3 Answers3

1

You can use list comprehension here.

pd.DataFrame({'sp':[d.get('sp') for d in filejson['data'] if d.get('sp')]})

   sp
0   2
1   3

You can use filter here.

pd.DataFrame({'sp': [*filter(None, map(itemgetter('sp'),filejson['data']))]})

   sp
0   2
1   3

Note:

filter(None, ...) removes all falsy values i.e False, 0, '', [] etc.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • can I read the json to numpy array and get all speed data point in that? – user269867 Jul 27 '20 at 04:27
  • @user269867 Not *NumPy expert* but AFAIK I don't think there's a function for it. Might be worth asking this as a new question. Might get an answer from NumPy experts. ;) – Ch3steR Jul 27 '20 at 04:30
1
filejson={"id": 103, "data": [{"point": [10, 20], "sp": 2}, {"point": [20, 20], "sp": 3}, {"point": [10, 20], "sp": 0}, {"point": [30, 20], "sp": 0}]}
df = pd.DataFrame(filejson['data'])

df = df.replace(0,np.nan)
df = df.dropna(how='all', axis=0)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
jaya
  • 11
  • 1
1

You could try

df = pd.DataFrame(filejson['data'])[['sp']]
df = df.replace(np.nan, 0)
df = df.loc[~(df.sp == 0), :]
minuscler
  • 34
  • 2
  • I like the idea to read from dataFrame directly instead of filtering but just curious , df.loc[~(df.sp == 0), :] --- what is this doing? – user269867 Jul 26 '20 at 19:54
  • That checks `sp` for values which are nonzero and filters the dataframe for those values – minuscler Jul 26 '20 at 22:47