简体   繁体   中英

Python: Modify the json type column in pandas dataframe

I am using python3 and pandas version 0.25. I have a JSON datatype in postgresql table. I am using pandas.io.sql to fetch the data from the table.

import pandas.io.sql as psql
df = psql.read_sql(sql,con,params=params)

So I am getting the dataframe from DB call as above.

When I check the output of the df (using IDE), I see the dataframe with following: 测向输出

Now, when I try to see the dtypes of the columns I see "object" for both the columns.

I want to update the columns and append value of the json, so it becomes: DF修改

I am unable to do the same.

I tried doing the below:

df.loc[:, 'col2_data'] = df.apply(lambda row: row['col2_data'].append({'multiplier':'2'}), axis=1)

But after the above statement, it is giving None for col2_data. ie is it not working.

Can someone help here?

Since each value is a list, we can use .update method of a dictionary to add new value. This might work in your case:

Method 1

df.loc['col2_data'] = df.apply(lambda row: [x.update({'multiplier':'2'}) for x in row['col2_data']], axis=1)

Method 2

df.loc['col2_data'] = df.apply(lambda row: [{**x, **{'multiplier':'2'}} for x in row['col2_data']], axis=1)

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