Home > Back-end >  Is there a way to extract contents from a JSON list inside a pandas dataframe cell?
Is there a way to extract contents from a JSON list inside a pandas dataframe cell?

Time:04-10

I have a column in a pandas dataframe that contains JSONs just like the example below. I want to extract just the zipcode value from either of the banks, but I can't.

[{"Bank1":{"zipcode":"045603", "total_amount":"400000"}}, {"Bank2":{"{"zipcode":"07069", "total_amount":"890000"}}]

I tried doing the following, which would work if it wasn't a JSON list, I believe:

df['bank1_zipcode'] = df['bank_data'].str['Bank1'].str['zipcode']

But had no success.

Thanks in advance!!!

CodePudding user response:

I think you can use map

df['bank_data'].map(lambda x: x['Bank1']['zipcode'])

CodePudding user response:

It's a list, try

df['bank1_zipcode'] = df['bank_data'].str[0].str['Bank1'].str['zipcode']
  • Related