Luiz Paulo Coutinho
Luiz Paulo Coutinho

Reputation: 3

how to extract values ​from a dictionary in the rows for the dataframe

I tried in several ways to extract the data from the adress column and transform it into new columns

 df.head(3)

       name    Adress

0      Joan   {'city': 'Rio', 'district': 'Leme'}

1      Joan   {'city': 'Sao Paulo', 'district': 'Bras'}

2   Vincent   {'city': 'Recife', 'district': 'Caxanga'}

I used all of these methods but none worked

#reduce(set.union, df_apresentar['address'], set())

#set(df_apresentar['address'].values())

#values = set(df_apresentar['address'][0])

#df_apresentar['endereco'] = df_apresentar.address.apply (lambda x: x.get ('address'))

#df_apresentar['endereco'] = df_apresentar.address.apply (lambda x: x.get ('value'))

I need something like that:

df.head(3)

       name    Adress  city     district

0      Joan   {}       Rio      Leme

1      Joan   {}      Friburgo  Bras

2   Vincent   {}      Recife    Caxanga

Upvotes: 0

Views: 839

Answers (3)

SeaBean
SeaBean

Reputation: 23237

You can use pd.json_normalize() to expand the json/dict into columns and then use pd.concat() to concatenate back to the original dataframe:

pd.concat([df.drop(columns='Adress'),  pd.json_normalize(df['Adress'])], axis=1) 


      name       city district
0     Joan        Rio     Leme
1     Joan  Sao Paulo     Bras
2  Vincent     Recife  Caxanga

Note that using pd.json_normalize() to expand the json/dict to columns is far more performance efficient than using .apply(pd.Series()) to expand.

Upvotes: 1

Anurag Dabas
Anurag Dabas

Reputation: 24322

You can also use Dataframe() method and tolist() method:

newdf=pd.DataFrame(data=df['Adress'].tolist())

Finally use concat() method:

newdf=pd.concat((df,newdf),axis=1)

Now If you print newdf you will get:

    name      Adress                                        city        district
0   Joan      {'city': 'Rio', 'district': 'Leme'}           Rio         Leme
1   Joan      {'city': 'Sao Paulo', 'district': 'Bras'}     Sao Paulo   Bras
2   Vincent   {'city': 'Recife', 'district': 'Caxanga'}     Recife      Caxanga

If needed use drop() method:

newdf=newdf.drop(columns=['Adress'])

Upvotes: 1

sacuL
sacuL

Reputation: 51425

You can use apply(pd.Series) to turn the address column dicts into their own columns, and concatenate that back in with the original df:

address_df = df.pop("Address")
new_df = pd.concat([df, address_df.apply(pd.Series)], axis = 1)

      name       city district
0     Joan        Rio     Leme
1     Joan  Sao Paulo     Bras
2  Vincent     Recife  Caxamga

Or, as a slight variation, use join instead of concat

new_df = df.drop("Address",axis=1).join(df.Address.apply(pd.Series))

Upvotes: 1

Related Questions