Home > Enterprise >  use specific columns to map new column with json
use specific columns to map new column with json

Time:06-02

I have a data frame with:

A B C
1 3 6

I want to take the 2 columns and create column D that reads {"A":"1", "C":"6}

new dataframe output would be:

A B C D
1 3 6 {"A":"1", "C":"6}

I have the following code:

df['D'] = n.apply(lambda x: x.to_json(), axis=1)

but this is taking all columns while I only need columns A and C and want to leave B from the JSON that is created.

Any tips on just targeting the two columns would be appreciated.

CodePudding user response:

Use subset in lambda function:

df['D'] = df.apply(lambda x: x[['A','C']].to_json(), axis=1)

Or sellect columns before apply:

df['D'] = df[['A','C']].apply(lambda x: x.to_json(), axis=1)

If possible create dictionaries:

df['D'] = df[['A','C']].to_dict(orient='records')
print (df)
    A   B   C                   D
0   1   3   6    {'A': 1, 'C': 6}

CodePudding user response:

It's not exactly what you ask but you can convert your 2 columns into a dict then if you want to export your data in JSON format, use df['D'].to_json():

df['D'] = df[['A', 'C']].apply(dict, axis=1)
print(df)

# Output
   A  B  C                 D
0  1  3  6  {'A': 1, 'C': 6}

For example, export the column D as JSON:

print(df['D'].to_json(orient='records', indent=4))

# Output
[
    {
        "A":1,
        "C":6
    }
]
  • Related