简体   繁体   中英

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

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))

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'])

You can use pd.json_normalize() to expand the json/dict into columns and then usepd.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM